我有一个关联到数据网格中的按钮的上下文菜单。我希望上下文菜单项根据我在视图模型中的字符串列表进行更改。当我单击按钮时,什么都没有显示。
这是我正在使用的 xaml,它位于数据网格中:
<Button Grid.Column="1" Content="..." Click="Button_Click">
<Button.ContextMenu>
<ContextMenu ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.SelectableDescriptions}">
<TextBlock Text="{Binding}"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
这是整个 DataGrid xaml:
<DataGrid Grid.Row="1" Grid.ColumnSpan="4" CanUserAddRows="True" AutoGenerateColumns="False" CanUserDeleteRows="True" ItemsSource="{Binding JobPricings, Mode=TwoWay}" SelectedItem="{Binding SelectedJobPricing, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Description" Width="25*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75*"/>
<ColumnDefinition Width="25*"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding Description,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="1" Content="..." Click="Button_Click">
<Button.ContextMenu>
<ContextMenu ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.SelectableDescriptions}">
<TextBlock Text="{Binding}"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Unit Price" Binding="{Binding UnitPrice, Mode=TwoWay}" Width="25*"/>
<DataGridTextColumn Header="Unit" Binding="{Binding Unit, Mode=TwoWay}" Width="25*"/>
<DataGridTemplateColumn Header="Currency " Width="25*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox SelectedValue="{Binding CurrencyID, Mode=TwoWay}" SelectedValuePath="ID" DisplayMemberPath="Description" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.Currencies}" ></ComboBox>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
这是我通过视图模型将上下文菜单绑定到的属性:
public ObservableCollection<string> SelectableDescriptions
{
get
{
_selectableDescriptions.Add("One");
_selectableDescriptions.Add("Two");
return _selectableDescriptions;
}
set
{
_selectableDescriptions = value;
}
}
关于为什么我的列表不会出现在上下文菜单中的任何想法?