我想知道如何将 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/