Сообщение Re: WPF. Отображение коллекции объектов с координатами от 30.11.2022 14:14
Изменено 30.11.2022 14:22 nekoriu
Re: WPF. Отображение коллекции объектов с координатами
N>Имеется коллекция точек с координатами. Как отобразить эти точки в соответствии с их координатами, используя биндинг в xaml, без code behind? В какую сторону копать?
Сделал, как было описано в статье, но почему-то окно белое и ничего на нем нет:
Сделал, как было описано в статье, но почему-то окно белое и ничего на нем нет:
public class ViewModel
{
public List<Item> Items { get; set; } = new List<Item>()
{
new Item {X = 100, Y = 200, Size=10, Text = "First"},
new Item {X = 500, Y = 300, Size=20, Text = "Second"},
new Item {X = 300, Y = 500, Size=30, Text = "Third"},
};
public int Width = 600;
public int Height = 600;
}
public class Item
{
public int X { get; set; }
public int Y { get; set; }
public int Size { get; set; }
public string Text { get; set; }
}
<Window x:Class="WPFClient.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFClient"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="600">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Viewbox Stretch="Uniform">
<ItemsControl ItemsSource="{Binding Items}" Width="{Binding Width}" Height="{Binding Height}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Silver" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}" />
<Setter Property="Canvas.Top" Value="{Binding Y}" />
</Style>
<DataTemplate DataType="{x:Type local:Item}">
<Rectangle Width="{Binding Size}" Height="{Binding Size}">
<Rectangle.Fill>
<SolidColorBrush Color="Gray" />
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</Viewbox>
</Window>
Re: WPF. Отображение коллекции объектов с координатами
N>Имеется коллекция точек с координатами. Как отобразить эти точки в соответствии с их координатами, используя биндинг в xaml, без code behind? В какую сторону копать?
Нашел решение: https://habr.com/ru/post/686438/.
Нашел решение: https://habr.com/ru/post/686438/.
public class ViewModel
{
public List<Item> Items { get; set; } = new List<Item>()
{
new Item {X = 100, Y = 200, Size=10, Text = "First"},
new Item {X = 500, Y = 300, Size=20, Text = "Second"},
new Item {X = 300, Y = 500, Size=30, Text = "Third"},
};
public int Width { get; set; } = 600;
public int Height { get; set; } = 600;
}
public class Item
{
public int X { get; set; }
public int Y { get; set; }
public int Size { get; set; }
public string Text { get; set; }
}
<Window x:Class="WPFClient.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFClient"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="600">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Viewbox Stretch="Uniform">
<ItemsControl ItemsSource="{Binding Items}" Width="{Binding Width}" Height="{Binding Height}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Silver" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}" />
<Setter Property="Canvas.Top" Value="{Binding Y}" />
</Style>
<DataTemplate DataType="{x:Type local:Item}">
<Rectangle Width="{Binding Size}" Height="{Binding Size}">
<Rectangle.Fill>
<SolidColorBrush Color="Gray" />
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</Viewbox>
</Window>