首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何绑定到ItemsPanel中的网格?

如何绑定到ItemsPanel中的网格?
EN

Stack Overflow用户
提问于 2015-07-14 22:13:29
回答 1查看 552关注 0票数 2

我正在构建一个基于ItemsControl的自定义控件。我正在尝试绑定到ItemsPanelTemplate中包含的网格。我似乎无法得到正确的绑定。任何帮助都是非常感谢的!

代码语言:javascript
复制
<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>

这是我的班级:

代码语言:javascript
复制
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")});
            }

        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-14 22:36:46

你当然可以!

使用此XAML:

代码语言:javascript
复制
<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>

使用以下代码:

代码语言:javascript
复制
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;
    }
}

祝你好运!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31418320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档