[英]Changing Style via data binding depending on CheckBox state
In one of my WPF user controls I have a check box. 在我的WPF用户控件之一中,我有一个复选框。 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. 谢谢。
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. 是的,您可以绑定到
IsChecked
并使用具有样式属性的Binding.Converter
并根据输入值返回其中一个。
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
}
Add following namespaces to your xaml: xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 将以下名称空间添加到您的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. 将控件上的默认样式设置为Style2。 Then assign a name to your control and add following trigger and action somewhere in your xaml (eg before you close your vf:DataSeries tag):
然后为您的控件分配一个名称,并在xaml中的某个位置添加以下触发器和操作(例如,在关闭vf:DataSeries标记之前):
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.