我正在构建基于ItemsControl
的自定义控件。我正在尝试绑定到ItemsPanelTemplate
中包含的网格。我似乎无法使绑定正确。任何帮助表示赞赏!
<local:MyItemsControl Height="500" Width="500" Margin="50" gRow="1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid Background="Red" >
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</local:MyItemsControl>
这是我的班级:
public class MyItemsControl : ItemsControl
{
public MyItemsControl()
{
this.DefaultStyleKey = typeof(ItemsControl);
}
public int gRow { get; set; }
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
if (element is FrameworkElement)
{
(element as ContentPresenter).SetBinding(Grid.RowProperty, new Binding {Source = item, Path = new PropertyPath("gRow")});
}
}
}