gpt4 book ai didi

c# - 具有网格布局的自定义 ItemsControl

转载 作者:太空宇宙 更新时间:2023-11-03 17:56:56 24 4
gpt4 key购买 nike

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

<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>

最佳答案

值被忽略,因为单元格不是 Grid 的直接子级,它们被包装在 ItemContainerGenerator 创建的 ContentPresenter 中的 ItemsControl

您需要使用 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>

关于c# - 具有网格布局的自定义 ItemsControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8546492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com