c# - WPF 绑定(bind)到不在 DataGrid ItemSsource 中的属性

标签 c# wpf xaml datagrid

我想知道如何将 DataGrid DataGridTemplateColumn 绑定(bind)到不在 DataGrid ItemSource 中但该属性位于 Itemsource 的同一 DataContext 中的属性?

XAML

        // I am trying to bind the Visibility property to a property called Visible
        <DataGridTemplateColumn Header="Apply" Visibility="{Binding source Visible}">

        // However the visible property doesnt exist inside the resource cvsCustomers
        ItemsSource="{Binding Source={StaticResource CustomerCollection}}"

C#

    // But they both live in the same ViewModel i.e. DataContext      
    private Visibility m_Visible = Visibility.Hidden;

    public Visibility Visible
    {
       get { return m_Visible; }
       set { m_Visible = value; }
    }

    private ObservableCollection<Customer> m_CustomerCollection = null;

    public ObservableCollection<Customer> CustomerCollection
    {
       get { return m_CustomerCollection; }
       set { m_CustomerCollection = value; }
    }

这能实现吗?

谢谢

最佳答案

Datagrid 列不在 DataGrid 的可视化树下。因此,您需要使用 BindingProxy 使 ViewModel 可供您的 DataGridTemplateColumn 访问。我在下面的答案中解释了如何创建和使用 BindingProxy:

Bind ViewModel property to DataGridComboBoxColum

设置 BindingProxy 后,您可以将 DataGridTemplateColumn 可见性绑定(bind)为

<DataGridTemplateColumn Header="Apply" Visibility="{Binding Path=Data.Visible, Source={StaticResource ProxyElement}" 

关于c# - WPF 绑定(bind)到不在 DataGrid ItemSsource 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23649747/

相关文章:

c# - 如何使用类似插件的架构来扩展 WPF 应用程序?

c# - 如何检查属性是否从自定义控件内部绑定(bind)

javascript - 如何在 C# Envelope->Error 中反序列化 XML

c# - 显然(!).NET 和 Mono 之间的签名不一致;单声道签名不是幂等的

c# - Server.MapPath 和 window.open()

c# - MSCharts 和 .NET 3.5 SP1

c# - UI无法更新foreach循环C#WPF中首先播放的声音

c# - MVVM跟踪 View 模型中的问题

c# - WPF 是否有浏览本地文件系统的控件?

c# - 使用 MVVM 模式从 View 模型到 WPF 中的 View 的条件绑定(bind)