WPF Button Style Based on Active View

I use Caliburn Micro to develop a simple MVVM MVVM application.

My ShellView has one ContentControl and three buttons, each of which is attached to a public method in my ShellViewModel, which allows you to use ActivateView1, ActivateView2 and ActivateView3.

My ShellViewModel inherits from explorer and every activation method call ActivateItem(new View1ViewModel()), etc.

So far so good. When I click the button, a new view is activated in ContentControl. The problem is that I need each button to change the style when its β€œlinked view” is active, and I really don't know how to achieve this functionality. Do you have any suggestions?

I am new to Caliburn Micro and WPF design, so any help would be greatly appreciated.

+3
source share
3 answers

I'm not very sure about this, but I can still think of something like that,

You can create a style and add style to your button. something like that

<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                         <Setter Property="Fill" TargetName="yourButtonName" Value="Black"/>
                         <Setter Property="Margin" TargetName="yourButtonName" Value="5,0,5,0"/>
                </Trigger>
            </Style.Triggers>
</Style>

and you can add this style to your button.

+1
source

I can imagine two possible options that you could use:

You can bind button style properties to properties on your ShellViewModel. In these properties you can define a style for return based on Active view shells i.e.

return ActiveItem == button1ViewModel ? 
                     (Style) App.Current.Resources["Button1ActiveStyleKey"] :
                     (Style) App.Current.Resources["Button1InactiveStyleKey"];

, ViewModel , , , . , Caliburn.Micro IResult, 3 ( ) Coroutine, , ..

public IEnumerable<IResult> ButtonOneClicked()
{
    yield return new ChangeButtonStyle("Button1Name", "Button1ActiveStyleKey");
    yield return new ChangeButtonStyle("Button2Name", "Button2InactiveStyleKey");
    yield return new ChangeButtonStyle("Button3Name", "Button3InactiveStyleKey");
}

ChangeButtonStyle IResult ( ActionExecutionContext IResult.Execute) , ChangeButtonStyle ctor, style , , ChangeButtonStyle ctor.

0

you can use

<Trigger Property ="IsPressed" Value ="True">

I think this is a trick ...

0
source

All Articles