我想知道如何将一个DataGrid DataGridTemplateColumn绑定到一个属性,该属性不在DataGrid的ItemSource中,但该属性在Itemsource的同一个DataContext中?WPF绑定到一个属性不在DataGrid中ItemSource
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; }
}
才能实现这一目标?
感谢
尽管你告诉我们,一个属性'CustomerCollection '在视图模型类中,您将ItemsSource绑定到驻留在ResourceDictionary中的集合中,并使用键“CustomerCollection”。这没有什么意义。 – Clemens
对不起 - 我解释说,集合来自CollectionViewSource,因为我需要在CustomerCollection上进行过滤。 – user3428422
好的。假设DataGrid的DataContext是ViewModel类的一个实例,您应该能够通过'Visibility =“{Binding Visible}”或'Visibility =“{Binding Path = Visible}”'绑定可见性。 – Clemens