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

标签 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; }
}

我遇到的问题是绑定(bind)到 MyCustomProperty。

如果我从 DataGrid 继承我的自定义控件并使用它的 Columns,DataContext 从 ItemsSource 流向 Bindings in columns 就好了。我想对不从 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 属性的绑定(bind)仍将与 DataGrid 父级的 DataContext 一起提供,为我提供项目。 MyAnotherCustomProperty 在设计器中会有波动,但它可以在运行时正常工作(因为动态绑定(bind))。我的问题是,当其他人使用此自定义 DataGridColumn 时,他/她需要知道这一点,并且绑定(bind)的 IntelliSense 会“错误”。

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

最佳答案

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

这是你设计中的问题..而且这里很难判断是对还是错。

DataGrid 和 GridView 中的 columns 属性或多或少只是一个虚拟对象,其中包含单元格稍后需要的值,例如单元格模板、绑定(bind)、宽度、高度...等等。可以这样说,列没有 true数据上下文...

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

单元格使用列中的那些占位符

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

希望这对你有帮助。

关于c# - 将 DataContext 级联到集合中的子元素的自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486417/

相关文章:

c# - 使用 MVVM,当控件没有 "Command"属性时怎么办?

c# - 无法解释的变量增加

c# - WCF aspNetCompatibilityEnabled ="true"引发异常(加载失败)

c# - 自动优化 Windows 窗体 InitializeComponent 的性能

wpf - 带有 DataGrid WPF 的复选框

c# - 重用具有不同 ViewModel 的 usercontrol(View)

iphone - ios - 我可以使用来自另一个 ViewController 的 IBAction 吗?

objective-c - 在我的自定义 NSSlider 中使用 CoreGraphics 进行 Cocoa 绘图时出现错误

c# - ESQL 不工作

c# - 如何检测 TFS 项目源代码已被修改