C# 在WPF中创建可单击的自定义图形
我正试着做一些看起来像这样的东西,但我不知道怎么做。 我有一个线程可以实时更新一个对象,告诉我三件事:numberPockets(5),drawerPosition(浅黄色),drawerTarget(深黄色)。所有托盘的高度和宽度都是固定的,因此如果引入更多的口袋,口袋大小将需要缩小C# 在WPF中创建可单击的自定义图形,c#,wpf,C#,Wpf,我正试着做一些看起来像这样的东西,但我不知道怎么做。 我有一个线程可以实时更新一个对象,告诉我三件事:numberPockets(5),drawerPosition(浅黄色),drawerTarget(深黄色)。所有托盘的高度和宽度都是固定的,因此如果引入更多的口袋,口袋大小将需要缩小 我还需要知道哪个口袋被点击,以便我知道去那个位置,并更新颜色。我在考虑使用某种经过修改的进度条,但我不知道它的点击位置,也不知道如何覆盖某种网格来显示不同的口袋。我也考虑过使用列表框,但在考虑实现时却一筹莫展。
我还需要知道哪个口袋被点击,以便我知道去那个位置,并更新颜色。我在考虑使用某种经过修改的进度条,但我不知道它的点击位置,也不知道如何覆盖某种网格来显示不同的口袋。我也考虑过使用列表框,但在考虑实现时却一筹莫展。如果您能提供一些指导,我们将不胜感激。我认为列表框可以工作。只需稍加设计,您就可以获得所需的行为。首先,您可能需要做以下重要的事情:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Title="Test" Height="650" Width="200">
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Rectangle Fill="White" Stroke="Black" StrokeThickness="1"/>
</ListBoxItem>
<ListBoxItem HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Rectangle Fill="Yellow" Stroke="Black" StrokeThickness="1"/>
</ListBoxItem>
<ListBoxItem HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Rectangle Fill="Yellow" Stroke="Black" StrokeThickness="1"/>
</ListBoxItem>
<ListBoxItem HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Rectangle Fill="DarkGoldenrod" Stroke="Black" StrokeThickness="1"/>
</ListBoxItem>
</ListBox>
</Window>
改为查看列表框。只需使用数据绑定集合中的项目填充它,并从ItemTemplate中的databound属性设置背景颜色。比我的建议好多了!