Hi,
this could be accomplished with binding. Use the following:
C# - ViewModels:
public class ListViewViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public List<ItemViewModel> ItemsSource
{
get;
set;
}
public ListViewViewModel()
{
this.ItemsSource = new List<ItemViewModel>();
this.ItemsSource.Add(new ItemViewModel
{
DisplayText = "ItemsSource #1"
});
this.ItemsSource.Add(new ItemViewModel
{
DisplayText = "ItemsSource #2"
});
this.ItemsSource.Add(new ItemViewModel
{
DisplayText = "ItemsSource #3"
});
}
}
public class ItemViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string DisplayText { get; set; }
public Visibility M1Visibility {
get {
return (!this.DisplayText.Contains("1")) ? Visibility.Visible : Visibility.Collapsed;
}
}
public Visibility M2Visibility {
get {
return (!this.DisplayText.Contains("2")) ? Visibility.Visible : Visibility.Collapsed;
}
}
}
XAML:
<ListView
ItemsSource="{Binding Path=ItemsSource}" DisplayMemberPath="DisplayText">
<ListView.ItemContainerStyle>
<Style
TargetType="{x:Type ListViewItem}">
<Setter
Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem
Header="MenueItem #1"
Visibility="{Binding Path=M1Visibility}" />
<MenuItem
Header="MenueItem #2"
Visibility="{Binding Path=M2Visibility}" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
I've made a few changes to your XAML for the sake clearness (this was at least my intention). There is a line of code missing, where the DataContext of the ListView's container is set to an instance of ListViewViewModel.
If you use this, you will see, that the ListItem #1 hides MenueItem #1, ListItem #2 hides MenueItem #2 and the last ListItem hides no MenueItem.
The key concept is, that the DataContext for the ContextMenue is inherited from the ListViewItem; so it is the point where to apply the visbility-logic. You can use some context-specific logic as I did or you can use the initialisers to provide the visibility values.
If you need further help, feel free to ask.
Cheers
Jürgen
--
If this saved you some time, please spend a bit of it to vote.