Hosea146
Hosea146

Reputation: 7702

Changing Style via data binding depending on CheckBox state

In one of my WPF user controls I have a check box. If the check box is not checked, I would like to use the following:

<vf:DataSeries Style="{StaticResource dataSeriesQuickLine}" ... >

However, if it is checked, I would like to use the following:

<vf:DataSeries Style="{StaticResource dataSeriesLine}" ... >

Is there a way I can bind the Style to the check box control to use the styling I want?

Thanks.

Upvotes: 1

Views: 1007

Answers (2)

roberther
roberther

Reputation: 446

Add following namespaces to your xaml: xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

Set the default style on your control to Style2. Then assign a name to your control and add following trigger and action somewhere in your xaml (e.g. before you close your vf:DataSeries tag):

 <i:Interaction.Triggers>
   <ei:DataTrigger  
       Binding="{Binding ElementName=yourCheckboxName, Path=IsChecked}"   
       Value="True">
       <ei:ChangePropertyAction TargetName="yourControlName"
                                 PropertyName="Style"
                                 Value="{StaticResource Style1}"/>
   </ei:DataTrigger>
 </i:Interaction.Triggers>

Upvotes: 1

brunnerh
brunnerh

Reputation: 184296

Yes, you could bind to IsChecked and use a Binding.Converter which has properties for the styles and returns either depending on the input value.

You could use a generic boolean converter:

<vc:BooleanConverter x:Key="StyleConverter"
                     TrueValue="{StaticResource Style1}"
                     FalseValue="{StaticResource Style2}"/>
public class BooleanConverter : IValueConverter
{
    public object TrueValue { get; set; }
    public object FalseValue { get; set; }

    // In Convert cast the value to bool and return the right property
}

Upvotes: 4

Related Questions