Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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# 具有网格布局的自定义项控件_C#_.net_Wpf_Xaml_Data Binding - Fatal编程技术网

C# 具有网格布局的自定义项控件

C# 具有网格布局的自定义项控件,c#,.net,wpf,xaml,data-binding,C#,.net,Wpf,Xaml,Data Binding,我有我的自定义项控件,用于显示数独游戏网格。我希望它将其项目显示到9X9网格。每个项都有X和Y属性,我想绑定到网格中的该属性位置(网格行和网格列)。除了绑定这些grid.row和grid.column属性外,其他一切看起来都正常。。代码如下。这不是绑定的错误,因为如果我使用硬值,什么都不会改变。请帮忙 <ItemsControl Margin="4" ItemsSource="{Binding Cells, Mode=OneWay}" x:Name="grid"> <

我有我的自定义项控件,用于显示数独游戏网格。我希望它将其项目显示到9X9网格。每个项都有X和Y属性,我想绑定到网格中的该属性位置(网格行和网格列)。除了绑定这些grid.row和grid.column属性外,其他一切看起来都正常。。代码如下。这不是绑定的错误,因为如果我使用硬值,什么都不会改变。请帮忙

<ItemsControl  Margin="4" ItemsSource="{Binding Cells, Mode=OneWay}" x:Name="grid">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
                <grid:GridCell Grid.Column="{Binding X}" Grid.Row="{Binding Y}"  />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
                <Grid IsItemsHost="True" Background="Pink">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition Height="2" />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition Height="2" />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition Width="2" />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition Width="2" />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

编辑: 例如,如果我使用

<ItemsControl.ItemTemplate>
    <DataTemplate>
            <grid:GridCell Grid.Column="2" Grid.Row="2"  />
    </DataTemplate>
</ItemsControl.ItemTemplate>

这些值被忽略,因为单元格不是
网格的直接子元素,它们被包装在
项控件的
项容器生成器创建的
内容呈现器中

您需要使用
ItemContainerStyle
在更高级别应用这些值

<ItemsControl.ItemContainerStyle>
    <Style TargetType="{x:Type ContentPresenter}">
        <Setter Property="Grid.Column" Value="{Binding X}" />
        <Setter Property="Grid.Row" Value="{Binding Y}" />
    </Style>
</ItemsControl.ItemContainerStyle>