首页 文章

自定义控件,将DataContext级联到集合中的子元素

提问于
浏览
7

我正在尝试创建自定义用户控件,其功能与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流向Bindings就好了 . 我想对我的自定义控件执行相同操作,该控件不从DataGrid继承 . DataGrid.Columns从ItemsSource获取上下文背后的魔力是什么?

Edit: 让我再问一下这个问题:

如果我实现自定义DataGridColumn

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时,他/她需要知道这一点,并且对于绑定会有“错误的”IntelliSense .

如何设置DataGridColumn的Binding属性的上下文,以便IntelliSense按预期工作?

2 回答

  • 2

    您的问题非常广泛但首先如果您希望扩展DataGrid,请考虑覆盖其样式并添加触发器或类似的东西 .

    它是你设计中的问题..而且很难说出这里的对错 .

    DataGrid和GridView中的columns属性或多或少只是一个虚拟对象,其中包含以后需要的单元格,例如单元格模板,绑定,宽度,高度......等等 . 让我们说这个列没有真正的DataContext . .

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

    单元格使用列中的占位符

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

    希望这对你有所帮助 .

  • -1

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

    现在我看到你的控件也来自ItemsControl . 然后我建议手动处理它 .

相关问题