WPF bindowanie, transformacja, odczyt danych

0

Witam,
Co prawda temat sugeruje kilka problemów, ale wątek - mam nadzieję - będzie rozwijany wraz z postępem mojej wiedzy. :)

Co chcę zrobić:

  • wczytać dane z kilku list, z których każda opisuje rodzaj obiektu (kółko, kwadrat), jego kolor oraz nazwę,
  • obiekty mają być "przesuwalne", obraz ma być skalowany bez skali wielkości elementów,
  • po kliknięciu na obiekt dane (nazwa) powinna być ładowana do kontrolek.

Utknąłem na samym początku, prosty kod - niestey nie działa :(

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<Rect> lista = new List<Rect>();

            Rect r1 = new Rect();
            r1.x = 200; r1.y = 200; r1.name = "rec1";
            Rect r2 = new Rect();
            r2.x = 300; r2.y = 250; r2.name = "rec2";

            lista.Add(r1);
            lista.Add(r2);
        }
    }
    public class Rect
    {
        public double x { set; get; }
        public double y { set; get; }
        public string name { set; get; }
    }

XAML:

    <Grid>
        <ItemsControl ItemsSource="{Binding list}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Rectangle Width="20" Height="20" Fill="Black">
                        <Rectangle.RenderTransform>
                            <TranslateTransform X="{Binding x}" Y="{Binding y}"/>
                        </Rectangle.RenderTransform>
                    </Rectangle>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>

0

Miałem kiedyś identyczny problem do pewnego mojego projektu i w rezultacie skończyło się na czymś takim:

<ItemsControl ItemsSource="{Binding Controls}">
	<ItemsControl.ItemsPanel>
		<ItemsPanelTemplate>
			<Canvas />
		</ItemsPanelTemplate>
	</ItemsControl.ItemsPanel>
	<ItemsControl.ItemTemplate>
		<DataTemplate>
			<Border Width="{Binding Width}" Height="{Binding Height}" MouseLeftButtonUp="Border_MouseLeftButtonUp" Opacity="0.7">
				<Border.Background>
					<SolidColorBrush Color="{Binding Background}" />
				</Border.Background>
				<TextBlock Text="{Binding ControlName}" HorizontalAlignment="Center" VerticalAlignment="Center" />
			</Border>
		</DataTemplate>
	</ItemsControl.ItemTemplate>
	<ItemsControl.ItemContainerStyle>
		<Style TargetType="ContentPresenter">
			<Setter Property="Canvas.Left" Value="{Binding Left}" />
			<Setter Property="Canvas.Top" Value="{Binding Top}" />
			<Setter Property="ToolTip" Value="{Binding ControlName}" />
		</Style>
	</ItemsControl.ItemContainerStyle>
</ItemsControl>

https://github.com/ktos/DjToKey/blob/devel/DjToKey/Views/MainWindow.xaml#L49-L72

0
    <StackPanel>
        <TextBox Name="MyTxt" Width="50" Text="{Binding ElementName=MySlider, Path=Value, Mode=TwoWay}"/>
        <Slider IsSnapToTickEnabled="True" Name="MySlider" Minimum="15" Maximum="25"/>
        <Button Content="Save" Click="Button_Click"/>
    </StackPanel>

Dziwne, jak klikam w pole textbox to mi się slider nie aktualizuje mimo "twoway"...

1 użytkowników online, w tym zalogowanych: 0, gości: 1