如何从GridView中绑定ViewModel的属性 [英] How to bind to a property of the ViewModel from within a GridView
问题描述
我使用MVVM设计模式,我的View的 DataContext
设置为ViewModel。
I'm using the MVVM design pattern, and the DataContext
of my View is set to a ViewModel.
在我的视图中,我有一个 ListView / GridView
与 ItemsSource
绑定到一个 DataTable
。其中一个 GridViewColumns
有一个 CellTemplate
,呈现一个 Button
。我希望按钮的 IsEnabled
属性绑定到ViewModel的 SelectButtonsEnabled
属性。
In my View, I have a ListView/GridView
with ItemsSource
bound to a DataTable
. One of the GridViewColumns
has a CellTemplate
that presents a Button
. I want the IsEnabled
property of the button to be bound to the SelectButtonsEnabled
property of my ViewModel.
使用 IsEnabled ={Binding Path = SelectButtonsEnabled}
不起作用,因为我的 DataTable
没有名为SelectButtonsEnabled的列。我试图绑定的属性是整个ViewModel的全局,不是特定于 DataTable
的一行。
Using IsEnabled="{Binding Path=SelectButtonsEnabled}"
doesn't work because my DataTable
doesn't have a column named "SelectButtonsEnabled". The property I'm trying to bind to is global for the whole ViewModel, not specific to a row of the DataTable
.
我想我需要某种 RelativeSource
标签,但我迄今为止的所有尝试都失败了。
I think I need some kind of RelativeSource
tag, but all my attempts thus far have failed.
谢谢。
推荐答案
嗯,我不知道这是最简洁的方法,但是这样做: / p>
Well, I don't know if this is the most succinct way to do it, but this works:
IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.SelectButtonsEnabled}"
这篇关于如何从GridView中绑定ViewModel的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!