具有级联到集合中的子元素的自定义控件

本文关键字:元素 自定义控件 集合 级联 | 更新日期: 2023-09-27 18:34:23

我正在尝试创建自定义用户控件,具有与DataGrid类似的功能(但DataGrid在这里不是正确的选项(。

我想要实现的是这样的目标:

<my:CustomList ItemsSource="{Binding Items}">
    <my:CustomList.Columns>
        <my:Column Width="60" Binding="{Binding MyCustomProperty}" />
    </my:CustomList.Columns>
</my:CustomList>

其中 Items 将来自 ViewModel(例如(,如下所示:

public ObservableCollection<Item> Items { get; set; }
public class Item
{
    public string MyCustomProperty { get; set; }
    public string MyAnotherCustomProperty { get; set; }
}

我遇到的问题是绑定到MyCustomProperty。

如果我从 DataGrid 继承我的自定义控件并使用其列,则 DataContext 从 ItemsSource 流向列中的绑定就好了。我想对我的自定义控件执行相同的操作,该控件不继承自 DataGrid。DataGrid.Columns 从 ItemsSource 获取上下文背后的魔力是什么?

编辑:让我问另一种方式:

如果我实现自定义数据网格列

public class MyDataGridColumn : DataGridBoundColumn
{
    private Binding _bindingSubText;
    public Binding BindingSubText
    {
        get
        {
            return _bindingSubText;
        }
        set
        {
            if (_bindingSubText == value) return;
            var oldBinding = _bindingSubText;
            _bindingSubText = value;
            OnBindingChanged(oldBinding, _bindingSubText);
        }
    }
    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var textTextBlock = new TextBlock();
        var bindingText = Binding ?? new Binding();
        textTextBlock.SetBinding(TextBlock.TextProperty, bindingText);
        var textSubTextBlock = new TextBlock();
        var bindingSubText = BindingSubText ?? new Binding();
        textSubTextBlock.SetBinding(TextBlock.TextProperty, bindingSubText);
        var stackPanel = new StackPanel() { Orientation = Orientation.Vertical };
        stackPanel.Children.Add(textTextBlock);
        stackPanel.Children.Add(textSubTextBlock);
        return stackPanel;
    }
    protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
    {
        // I don't want to edit elements
        return null;
    }
}

并尝试在 XAML 中使用它,如下所示:

<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <my:MyDataGridColumn Binding="{Binding MyCustomProperty}" BindingSubText="{Binding MyAnotherCustomProperty}" />
    </DataGrid.Columns>
</DataGrid>

BindingSubText 属性的绑定仍然与 DataGrid 父级的 DataContext 一起提供,为我提供 Items。MyAnotherCustomProperty 在设计器中会有摆动,但它在运行时可以正常工作(因为动态绑定(。我的问题是,当其他人使用此自定义 DataGridColumn 时,他/她需要知道这一点,并且绑定的智能感知会"错误"。

如何设置数据网格列的绑定属性的上下文,以便智能感知按预期工作?

具有级联到集合中的子元素的自定义控件

你的问题真的很广泛,但首先,如果你想扩展DataGrid,请考虑覆盖它的风格并添加触发器或类似的东西。

这是你设计中的问题......很难分辨这里的对错。

DataGrid 和 GridView 中的列属性或多或少只是一个虚拟对象,其中包含单元格稍后将需要的值,例如单元格模板、绑定、宽度、高度......等。让我们说,这个列没有真正的数据上下文...

数据上下文沿功能可视化/逻辑树向下传递,但列属性不存在。这就是为什么你有你的问题。您可以在此类列对象中设置的所有绑定实际上是占位符。然而,单元格参与可视化树,因为它们是自己的控件,所以一旦生成一个单元格,它们在被绘制之前会做这样的事情:cellTextBlock.SetBinding(TextBlock.TextProperty,columnBindingPlaceHolder(,cellTextBlock.SetBinding(TextBlock.HeightProperty,columnHeightPlaceHolder(。

单元格使用列中的占位符

但是,如果您希望使用自己的自定义列属性与 DataGrid 具有相同的 DataContext,请考虑将 ObseravableCollection 更改为 FreezableCollection。同时使列对象成为可冻结的对象。只需使用冷冻品即可。因为在 WPF 中,它们能够继承 DataContext。例如,WPF中的画笔是可冷冻的。

希望这对你有帮助。

我认为,DataGrid的祖先之一(可能是ItemsControl(处理这个问题。如果控件不是从 ItemsControl 派生的,则必须手动处理此问题(向控件添加新列时,显式设置其数据上下文(。

现在我看到您的控件也派生自 ItemsControl。然后我建议手动处理它。