我需要使用Grid作为PanelTemplate。这不是什么大不了的事,但我也需要在Grid中有其他的控件。所以我的ItemsControl是这样的:
<ItemsControl x:Name="itemscontname" ItemsSource="{Binding Fields}" Grid.Column="0"
ItemsPanel="{StaticResource gridKey}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button .../>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Style.Setters>
<Setter Property="Grid.Row" Value="{Binding RowNumber}" />
<Setter Property="Grid.Column" Value="{Binding ColumnNumber}" />
</Style.Setters>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>我的ItemsPanelTemplate:
<ItemsPanelTemplate x:Key="gridKey">
<Grid Grid.Row="0" Grid.Column="0" >
<Grid.RowDefinitions>
....
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
....
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>很好,很管用。但是在这个Grid中,我想要一个,比如说一个TextBlock。
<ItemsPanelTemplate x:Key="gridKey">
<Grid Grid.Row="0" Grid.Column="0" >
<Grid.RowDefinitions>
....
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
....
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
Text="{Binding SomeTextFromVM}"/>
</Grid>
</ItemsPanelTemplate>当我这么做的时候,它会破坏一切。我该怎么解决这个问题?
发布于 2016-03-19 14:47:07
您通常会在Template of ItemsControl中添加元素,除了ItemsPanel模板之外,还会添加元素。
但是,不能将这些元素添加到ItemsPanel中的网格(由ItemsPresenter管理)。
<ItemsControl ...>
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Grid>
...
<ItemsPresenter/>
<TextBlock Grid.Row="0" Grid.Column="0"
Text="{Binding SomeTextFromVM}"/>
</Grid>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
...
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
...
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
...
</ItemsControl.ItemContainerStyle>
</ItemsControl>https://stackoverflow.com/questions/36102800
复制相似问题