I want to have a Dictionary<string , SomeView>
so that i display the keys in the menu and when someone clicks the menuitem , the corrosponding SomeView
is injected into a ContentControl
I have:
<Menu Name="menu"/>
<ContentControl Name="content"/>
and:
public partial class TestWindow : Window
{
Dictionary<string, TextBlock> menuItems = new Dictionary<string,TextBlock>();
public TestWindow()
{
InitializeComponent();
menuItems.Add("sign-in", new TextBlock(){Text="sign in"});
menuItems.Add("register", new TextBlock(){Text="sign up"});
menu.ItemsSource = menuItems.Keys;
}
}
how do I get the menu item click event and get the corrosponding string to that menu item.
IF YOU ARE USING MVVM, THIS IS NOT THE RIGHT WAY TO DO THINGS
Xaml:
<Menu >
<Menu.Resources>
<Style TargetType="{x:Type MenuItem}">
<EventSetter Event="Click" Handler="ContextMenuClick" />
</Style>
</Menu.Resources>
<MenuItem Header="_File" Name="File">
<MenuItem Name="Hello" Header="_Hello"/>
<MenuItem Name="Never" Header="_Never"/>
<MenuItem Name="Do" Header="_Do "/>
<MenuItem Name="This" Header="_This"/>
</MenuItem>
</Menu>
C#:
private void ContextMenuClick(object sender, RoutedEventArgs e)
{
MessageBox.Show(string.Format("MyContent:{0}", ((MenuItem) sender).Name));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.