开发者

WPF button styling depending on active view

I'm using Caliburn Micro to develop a simple MVVM WPF application.

My ShellView has a single ContentControl and three buttons each which bind to a public method in my ShellViewModel, lets say ActivateView1, ActivateView2 and ActivateView3.

My ShellViewModel inherits from Conductor and each Activate method calls ActivateItem(new View1ViewModel()), etc.

So far so good. When I click a button, a new view gets activated in the ContentControl. The problem is that I need each button to change style when its "associated view" is active and I have really no idea how to ach开发者_StackOverflowieve this functionality. Do you have any suggestions?

I'm fairly new to Caliburn Micro and WPF-styling so any help will be much appreciated.


I am not very sure about this but still I can think of something like this,

you can create a style and add the style to your button. something like this

<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.


I can think of two possible options you could use:

You could bind your buttons style properties to properties on you ShellViewModel. In those properties you can determine the style to return based on the shells Active view i.e.

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

this would mean your ViewModel would be aware of Styles which you may not want. If that is the case option two would be to write a Caliburn.Micro IResult which changes the style of the button and return 3 of those (one for each button) from a Coroutine that is invoked via the button click i.e.

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

The implementation of the ChangeButtonStyle IResult would search the view (provided via the ActionExecutionContext parameter to IResult.Execute) for a control with the name provided to the 1st parameter of ChangeButtonStyle ctor, and then set the style property of that control using the resource key provided as the 2nd paramter to the ChangeButtonStyle ctor.


You can use

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

I think it does the trick...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜