I'm trying to set a property of an object when a specific MenuItem is chosen. For example, If the 'Start' MenuItem is chosen, the property should be set to TRUE. If a different MenuItem is chosen such as 'Stop', the property should be set to FALSE. I also set up a Command binding to start a separate window when one of the MenuItems is chosen. The command binding works and starts the separate window when the "Start" MenuItem is chosen. However, the property does not go to TRUE when "Start" is chosen.
This is what I have so far:
<Window x:Class="ServerWindows.ServerMenuBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:server="clr-namespace:ServerWindows;assembly=ServerWindows"
Title="Server Window" Height="65" Width="650" MaxWidth="650" MaxHeight="65" MinHeight="65" MinWidth="650" ResizeMode="NoResize">
<Window.Resources>
<Style x:Key="ServerStarted" TargetType="MenuItem">
<Style.Triggers>
<DataTrigger Binding="{Binding server:ServerMenuCommands.ServerStarted}" Value="true">
<Setter Property="Tag" Value="true"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="ServerStopped" TargetType="MenuItem">
<Style.Triggers>
<DataTrigger Binding="{Binding server:ServerMenuCommands.ServerStarted}" Value="false">
<Setter Property="Tag" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<MenuItem FontSize="14" Header="Start" Command="server:ServerMenuCommands.OpenPuttyDisplayCommand" Style="{StaticResource ServerStarted}"/>
<MenuItem FontSize="14" Header="Stop" Style="{StaticResource ServerStopped}"/>
</Grid>
</Window>
The class definition combines the data property with the command bindings in a static class as such:
public static class ServerMenuCommands
{
public static bool ServerStarted
{
get;
set;
}
public static readonly RoutedUICommand OpenPuttyDisplayCommand = new RoutedUICommand("Open Putty Display", "PUTTY_DISPLAY", typeof(ServerMenuCommands));
}
Finally, the use of the property is done in the window started by the Command Binding:
if (ServerMenuCommands.ServerStarted)
{
puttyText.AppendText("\n\rServer STARTED");
puttyText.ScrollToEnd();
}
else
{
puttyText.AppendText("\n\rServer STOPPED");
puttyText.ScrollToEnd();
}
Again. I just want to set the above property so I can emit a notification to the user the choice of MenuItem "Start" or "Stop" by updating the separate window started by the Command Binding.
Thanks in advance for any help on this.
EDIT
Well. After trying the answer below with several other tweaks to what is outlined below, I am unable to get what I need working. It seems that maybe there is a misunderstanding of what I want to accomplish. I want to set a property of a static object based on a choice of a particular menu item control. It seems that examples I see work the other way around. Set a property of a control based on the value of a Property (field) of an object. That is why I did not create an event in my static class. I don't want to send a PropertyChanged event anywhere. I want to set the field to a value (True or False). Thanks for the potential solution to my problem. It looks like I'll just assign Click callbacks to the Menu Items and set the object Property that way.
Edit:Edit I am finally able to get this working based on the edited answer below. The key changes for me was to add the Command Bindings in the XAML and call the command method that changes the static property. Also key was the static event and tying it to the static property setter and then tying it to the Trigger in the XAML. It's all good all around!