binding - 如何绑定(bind)到 ItemsPanel 中的网格?

标签 binding grid winrt-xaml itemscontrol

我正在构建一个基于ItemsControl的自定义控件。我正在尝试绑定(bind)到 ItemsPanelTemplate 中包含的网格。我似乎无法正确绑定(bind)。如有任何帮助,我们将不胜感激!

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

这是我的类(class):

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")});
            }

        }
    }

最佳答案

你一定可以!

使用此 XAML:

<local:MyItemsControl>

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Text}" 
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        Grid.RowSpan="{Binding RowSpan}" 
                        Grid.ColumnSpan="{Binding ColumnSpan}"
                        Grid.Row="{Binding Row}" 
                        Grid.Column="{Binding Column}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <local:MyItem Text="One" Row="0" RowSpan="1" Column="0" ColumnSpan="1" />
    <local:MyItem Text="Two" Row="1" RowSpan="1" Column="0" ColumnSpan="1" />
    <local:MyItem Text="Three" Row="0" RowSpan="1" Column="1" ColumnSpan="1" />
    <local:MyItem Text="Four" Row="1" RowSpan="1" Column="1" ColumnSpan="1" />

</local:MyItemsControl>

使用此代码:

public class MyItem
{
    public string Text { get; set; }
    public int Row { get; set; }
    public int RowSpan { get; set; }
    public int Column { get; set; }
    public int ColumnSpan { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

public class MyItemsControl : ItemsControl
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        var container = base.GetContainerForItemOverride() as FrameworkElement;
        if (container == null) return container;

        var content = ItemTemplate.LoadContent() as FrameworkElement;
        if (content == null) return container;

        // sync the container grid dependency properties with the content
        var binding = content.GetBindingExpression(Grid.RowProperty);
        if (binding != null)
            container.SetBinding(Grid.RowProperty, content.GetBindingExpression(Grid.RowProperty).ParentBinding);
        binding = content.GetBindingExpression(Grid.RowSpanProperty);
        if (binding != null)
            container.SetBinding(Grid.RowSpanProperty, binding.ParentBinding);
        binding = content.GetBindingExpression(Grid.ColumnProperty);
        if (binding != null)
            container.SetBinding(Grid.ColumnProperty, binding.ParentBinding);
        binding = content.GetBindingExpression(Grid.ColumnSpanProperty);
        if (binding != null)
            container.SetBinding(Grid.ColumnSpanProperty, binding.ParentBinding);

        return container;
    }
}

祝你好运!

关于binding - 如何绑定(bind)到 ItemsPanel 中的网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31418320/

相关文章:

c# - WPF MVVM CanExecute方法实现问题

java - 将 java 类作为闭包绑定(bind)到 groovy 脚本中

c# - WPF,为什么我的绑定(bind)只从 MainWindow 更新?

twitter-bootstrap - Bootstrap 网格与折叠容器之间

c# - 强制枢轴项目在显示之前预加载

objective-c - 使用操作和状态变量连接多个 NSMenuItem

javascript - KendoUI MVVM 绑定(bind)根据显示值手动更新源

angular - DevExtreme 隐藏列 EditMode Angular2

windows-8 - 在保持触摸选择项目的能力的同时启用鼠标滚动

c# - XAml Windows 应用商店应用程序中的表