使用StyleSelector作为按钮

dan*_*dax 16 .net wpf

我需要根据数据中的值更改按钮的样式.看起来StyleSelector可以完美地工作,但似乎没有办法为按钮设置一个.

有没有办法从数据动态设置按钮样式?也许甚至是纯粹的XAML方法?

Skr*_*sli 27

完成同样事情的更通用的方法:

SomeView.xaml

<UserControl>
  <UserControl.Resources>
    <converters:BooleanToStyleConverter x:Key="MyButtonStyleConverter" 
      TrueStyle="{StaticResource AmazingButtonStyle}" 
      FalseStyle="{StaticResource BoringButtonStyle}"/>
  </UserControl.Resources>
  <Grid>
    <Button Style={Binding IsAmazingButton, Converter={StaticResource MyButtonStyleConverter}}/>
  </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

BooleanToStyleConverter.cs

public class BooleanToStyleConverter : IValueConverter
{
    public Style TrueStyle { get; set; }
    public Style FalseStyle { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool && (bool) value)
        {
            return TrueStyle;
        }
        return FalseStyle;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

只要您绑定到ViewModel中的布尔属性来控制样式切换,此转换器就可以在任何视图中使用您选择的任何样式进行任何控制.尽管如此,它很容易适应其他装订要求.这也适用于DataTemplate.

  • 最后,一种基于绑定数据有条件地设置样式的漂亮,简单,优雅的方式 - 我无法相信这需要花费很长时间才能找到.谢谢. (2认同)

Fre*_*lad 23

您可以将Button样式放在资源字典中并绑定样式Button并使用转换器

ButtonStyles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="ButtonStyle1" TargetType="Button">
        <Setter Property="Background" Value="Green"/>
        <Setter Property="FontSize" Value="12"/>
    </Style>
    <Style x:Key="ButtonStyle2" TargetType="Button">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

然后,对于Button具有此要求的那个,您将绑定Style到感兴趣的属性

<Button ...
        Style="{Binding Path=MyDataProperty,
                        Converter={StaticResource ButtonStyleConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

在转换器中,您加载ButtonStyles资源字典并根据值返回所需的样式

public class ButtonStyleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Uri resourceLocater = new Uri("/YourNameSpace;component/ButtonStyles.xaml", System.UriKind.Relative);
        ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
        if (value.ToString() == "Some Value")
        {
            return resourceDictionary["ButtonStyle1"] as Style;
        }
        return resourceDictionary["ButtonStyle2"] as Style;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)