从这个问题(DataTemplateSelector for Uniform Grid binding?)开始,我仍然没有让我的按钮/文本块坚持到视图模型中它们绑定到的行和列。我只会发布XAML,因为我知道行和列绑定是正确的(例如,Live Visual Tree告诉我row是1,column是1,但在网格上它显示的并非如此...)。
如果你需要更多的代码,请让我知道。这是XAML的一小部分。
<Grid DockPanel.Dock="Left" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="800" Height="400">
<ItemsControl ItemsSource="{Binding ObjCompositeCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid DockPanel.Dock="Top" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Name="objGrid" Grid.Row="1"
Rows="{Binding RowCount}"
Columns="{Binding ColumnCount}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.Column" Value="{Binding Column}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type engine:ObjA}">
<Button Content="{Binding Id}" />
</DataTemplate>
<DataTemplate DataType="{x:Type engine:GridLabeller}">
<TextBlock Text="{Binding HeaderName}"/>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</Grid>发布于 2019-09-13 22:31:39
WPF中的UniformGrid并不关心Grid.Row和Grid.Column附加属性。您需要使用RowDefinitions和ColumnsDefinitions创建一个Grid,以便将这些属性设置为有效。
您可以基于RowCount和ColumnCount源代码属性的值在视图中以编程方式创建Grid。
https://stackoverflow.com/questions/57925217
复制相似问题