I am trying to make a list with a context menu using a data template. So far I have the following code:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<sys:String>Item 1</sys:String>
<sys:String>Item 2</sys:String>
</ListBox.Items>
</Listbox>
I want to add the following context menu:
<ContextMenu>
<MenuItem Header="Edit" />
<MenuItem Header="Delete" />
</ContextMenu>
I have tried using ListBox.ItemContainerStyle and Setter but I am using Material Design, and when I use a style setter it takes away those material design styles.
Solution:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="Transparent">
<TextBlock Text="{Binding}"/>
</StackPanel>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit" />
<MenuItem Header="Delete" />
</ContextMenu>
</StackPanel.ContextMenu>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<sys:String>Item 1</sys:String>
<sys:String>Item 2</sys:String>
</ListBox.Items>
</Listbox>
Or use BasedOn property with ListBox.ItemContainerStyle.