Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中创建可单击的自定义图形_C#_Wpf - Fatal编程技术网

C# 在WPF中创建可单击的自定义图形

C# 在WPF中创建可单击的自定义图形,c#,wpf,C#,Wpf,我正试着做一些看起来像这样的东西,但我不知道怎么做。 我有一个线程可以实时更新一个对象,告诉我三件事:numberPockets(5),drawerPosition(浅黄色),drawerTarget(深黄色)。所有托盘的高度和宽度都是固定的,因此如果引入更多的口袋,口袋大小将需要缩小 我还需要知道哪个口袋被点击,以便我知道去那个位置,并更新颜色。我在考虑使用某种经过修改的进度条,但我不知道它的点击位置,也不知道如何覆盖某种网格来显示不同的口袋。我也考虑过使用列表框,但在考虑实现时却一筹莫展。

我正试着做一些看起来像这样的东西,但我不知道怎么做。

我有一个线程可以实时更新一个对象,告诉我三件事:numberPockets(5),drawerPosition(浅黄色),drawerTarget(深黄色)。所有托盘的高度和宽度都是固定的,因此如果引入更多的口袋,口袋大小将需要缩小


我还需要知道哪个口袋被点击,以便我知道去那个位置,并更新颜色。我在考虑使用某种经过修改的进度条,但我不知道它的点击位置,也不知道如何覆盖某种网格来显示不同的口袋。我也考虑过使用列表框,但在考虑实现时却一筹莫展。如果您能提供一些指导,我们将不胜感激。

我认为列表框可以工作。只需稍加设计,您就可以获得所需的行为。首先,您可能需要做以下重要的事情:

  • 覆盖列表框的ItemsPanel以使用UniformGrid
  • 禁用列表框中的滚动
  • 确保ListBoxItems的ContentAlignment设置为Stretch
  • 见下面的示例。它可能会帮助你开始

    <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属性设置背景颜色。比我的建议好多了!