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

时间:2014-12-15 14:36:14

标签: c# wpf custom-controls datacontext

我尝试创建自定义用户控件,其功能与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继承我的自定义控件并使用它的Columns,DataContext就会从ItemsSource流向Bindings就好了。我想对我的自定义控件做同样的事情,它不会从DataGrid继承。 DataGrid.Columns从ItemsSource获取上下文背后的魔力是什么?

修改 让我再问一下这个问题:

如果我实现自定义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,为我提供项目。 MyAnotherCustomProperty在设计器中会有摆动,但它可以正常运行(因为动态绑定)。我的问题是,当其他人将使用这个自定义DataGridColumn时,他/她将需要知道这一点,并且会发生错误&#34;用于绑定的IntelliSense。

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

2 个答案:

答案 0 :(得分: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中的画笔是freezables。

希望这对你有所帮助。

答案 1 :(得分:-1)

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

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