1

I have a menu button that opens a window when clicked.

I want to change it's color when it is clicked and revert when the window is closed.

I thought to use trigger to change it's color when clicked but how can i know when the window was closed to change the color back?

i need it in mvvm way so i prefer not using code behind

5 Answers 5

2

One of the patterns that go hand in hand with a view model is the Event Aggregator. The basic gist is that you create a message bus that allows interested objects to subscribe to specific events that other objects might fire without requiring a direct coupling between the two.

Your main view model might subscribe to a WindowClosed event (that provides an identifier for determining which specific window has closed). When your dialog has closed, its view model would be responsible for firing the event.

When your view model receives the event it would then update the property, that your menu item's trigger is bound to.

Sign up to request clarification or add additional context in comments.

Comments

1

if you use mvvm then you hopefully use a IWindowService to open your window. if you do so your viewmodel knows when the service/window close and you can use a boolean to bind to your trigger.

Comments

1

I am assuming that you are instantiating and the second window from within the ViewModel that is bound to the button's Command property. This would typically be how this situation would be handled in MVVM. This allows you to create another property within your ViewModel that queries whether the window is open or not.

private Window _secondWindow = null; public bool SecondWindowIsOpen { get { return _secondWindow != null; } } 

Then in your XAML you can bind the button's BackgroundColor to this property and use Triggers to determine the color.

<Window.Resources> <Style x:Key="WindowIsOpenButtonStyle" TargetType="{x:Type Button}"> <Style.Triggers> <DataTrigger Binding="{Binding SecondWindowIsOpen}" Value="True"> <Setter Property="Background" Value="Green" /> </DataTrigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Button Background="Yellow" Command="{Binding OpenSecondWindow}" Style="{StaticResource WindowIsOpenButtonStyle}" /> </Grid> 

Now, when the window is open the button's background color will change to Yellow, and when it is closed again it will be revert back to it's original color.

Comments

0

You could use a togglebutton, with the IsChecked property bound to some boolean that maintains the state of the whether the window is open/closed.

Hope that helps

1 Comment

How can i know if the window is closed to update the bool property if i using mvvm
0

You can subscribe to the Closed event of your window and in that event you can set your any bool property like this -

private void Border_MouseEnter(object sender, MouseEventArgs e) { Window w = new Window(); w.Closed += new EventHandler(w_Closed); } void w_Closed(object sender, EventArgs e) { // Set your property here to true } 

Simply, you can play with that property in your Trigger to change color of your menu item.

1 Comment

Tanks for the answer but i search for xaml / mvvm way. I update the question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.