如何绑定到ItemsPanel中的网格?

时间:2015-07-14 22:13:29

标签: binding grid winrt-xaml itemscontrol

我正在构建基于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")});
            }

        }
    }

0 个答案:

没有答案