坚持记录实属不易,希望友善多金的码友能够随手点一个赞。
共同创建氛围更加良好的开发者社区!
谢谢~
创建一个Converter,返回对应的style实现对应的修改
创建多个样式
将创建的样式与Converter的属性绑定
利用Binding 将vm中的属性作为Converter的传参实现动态样式功能
创建一个Converter根据传参,返回相应的样式
public class ConditionToStyleConverter : IValueConverter
{
public Style CurrentStyle { get; set; }
public Style OptionalStyle { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? CurrentStyle : OptionalStyle ;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return new InvalidOperationException();
}
}
}
<Grid.Resources>
<Style x:Key = "FirstStyle" TargetType="{x:Type Button}" BasedOn = "{StaticResource OptionalStyle}"/>
<Style x:Key = "FirstStyle" TargetType="{x:Type Button}" BasedOn = "{StaticResource CurrentStyle}"/>
<local:ConditionToStyleConverter
x:Key = "ConditionToStyleConverter"
CurrentStyle = "{StaticResource CurrentStyle}"
OptionalStyle = "{StaticResource OptionalStyle}"/>
</Grid.Resources>
<Button Style="{Binding IsSucceed}",Converter = {StaticResource ConditionToStyleConverter}"/>