我想知道如何将DataGrid DataGridTemplateColumn绑定到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; }
}
这可以实现吗?
由于
答案 0 :(得分:4)
Datagrid列不在DataGrid
的可视树下。因此,您需要使用BindingProxy来使DataGridTemplateColumn
可以访问ViewModel。我已在下面的答案中解释了如何创建和使用BindingProxy
:
Bind ViewModel property to DataGridComboBoxColum
设置BindingProxy后,您可以将DataGridTemplateColumn可见性绑定为
<DataGridTemplateColumn Header="Apply" Visibility="{Binding Path=Data.Visible, Source={StaticResource ProxyElement}"