【发布时间】:2020-03-05 08:55:58
【问题描述】:
我的 WPF 上有一个 GirdControl,它绑定到 NoteFrontEnd 类型的对象。在NoteFrontEnd 中有一个名为NoteType 的属性,我想将其用作MenuItem 中可见性绑定的来源。
用户必须右键单击GridControl 中的NoteFrontEnd 对象之一,并根据其NoteType 属性,使用Header="Process Item" 显示或隐藏MenuItem。
GridControl 和 MenuItem 在 xaml 中定义为:
<dxg:GridControl Name="GridCtrl"
ItemsSource="{Binding Path=BaseDashboardDataSource}"
SelectedItems="{Binding SelectedItems, Mode=TwoWay}"
AutoGenerateColumns="None">
<dxg:GridControl.Columns>
...
<dxg:GridColumn x:Name="NoteType" FieldName="NoteType" Header="Type" />
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView.ContextMenu>
<ContextMenu>
<ContextMenu.ItemsSource>
<CompositeCollection>
...
<!--Menu Item to toggle visibility of-->
<MenuItem Header="Process Item"
Visibility="{Binding ElementName=GridCtrl, Path=SelectedItem, Converter={StaticResource GVItemToVis}}"
Command="{...}">
</MenuItem>
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu>
</dxg:TableView.ContextMenu>
</dxg:TableView>
</dxg:GridControl.View>
</dxg:GridControl>
我的 ViewModel 是这样定义的:
public class NoteViewModel : DashboardViewModelBase
{
...
public ObservableCollection<NoteFrontEnd> BaseDashboardDataSource { get; private set; }
public ObservableCollection<BrokerNoteFrontEnd> SelectedItems
{
get { return _selectedItems; }
set
{
if (_selectedItems == value) return;
_selectedItems = value;
OnPropertyChanged();
}
}
public NoteViewModel(...) {
...
BaseDashboardDataSource = new ObservableCollection<NoteFrontEnd>();
}
}
和NoteFrontEnd 一样:
public class NoteFrontEnd
{
public string NoteType { get; set; }
}
我收到以下错误:
Cannot find source for binding with reference 'ElementName=GridCtrl'. BindingExpression:Path=SelectedItems;...
我尝试过类似下面的其他绑定,但得到了同样的错误:
Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Grid}, Path=PlacementTarget.SelectedItem, Converter={StaticResource GVItemToVis}}"
我怎样才能让这个绑定起作用?
【问题讨论】: