绑定WPF样式 [英] Binding for WPF Styles

查看:174
本文介绍了绑定WPF样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义控件 - 一个按钮 - 根据数据上下文中的属性值,它将应用多种样式。

I'm trying to create a custom control - a button - which will have multiple styles applied to it depending on the value of a property within the data context.

我在想的是使用类似的东西:

What I was thinking is using something similar to:

<Button Style="{Binding Path=ButtonStyleProperty, Converter={StaticResource styleConverter}}" Text="{Binding Path=TextProp}" />

而在代码中...实现一个IValueConverter,它类似于 ConvertTo 方法:

And in code... Implement an IValueConverter which does something similar to the code below in the ConvertTo method:

switch(value as ValueEnums)
{
    case ValueEnums.Enum1:
        FindResource("Enum1ButtonStyle") as Style;
    break;

    ... and so on.
} 

然而,我不完全确定如何拉出样式对象即使这是可能的...

However I'm not entirely sure about how to pull out the style object and even if this is possible at all...

我在同一时间做的是处理 DataContextChanged 事件,然后将一个处理程序附加到绑定到该按钮的对象的 PropertyChanged 事件 - 然后在其中运行switch语句。

What I am doing in the mean time is handling the DataContextChanged event, then attaching a handler to the PropertyChanged event of the object being bound to the button - then running the switch statement in there.

它不是很完美,但直到找到一个更好的解决方案,这似乎是我必须使用的。

Its not quite perfect but until I can find a better solution it seems like that is what I'll have to use.

推荐答案

如果要替换整个样式(而不仅仅是元素),那么您可能会将这些样式存储在资源中。您应该能够按照以下方式执行某些操作:

If you want to replace the whole style (rather than just elements of it) then you'll probably be storing those styles in resources. You should be able to do something along the lines of:

<Button>
    <Button.Style>
        <MultiBinding Converter="{StaticResource StyleConverter}">
            <MultiBinding.Bindings>
                <Binding RelativeSource="{RelativeSource Self}"/>
                <Binding Path="MyStyleString"/>
            </MultiBinding.Bindings>
        </MultiBinding>
    </Button.Style>
</Button>

通过使用MultiBinding并使用Self作为第一个绑定,我们可以在我们的转换器中查找资源。转换器需要实现IMultiValueConverter(而不是IValueConverter),可以看起来像这样:

By using a MultiBinding and using Self as the first binding we can then lookup resources in our converter. The converter needs to implement IMultiValueConverter (rather than IValueConverter) and can look something like this:

class StyleConverter : IMultiValueConverter 
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        FrameworkElement targetElement = values[0] as FrameworkElement; 
        string styleName = values[1] as string;

        if (styleName == null)
            return null;

        Style newStyle = (Style)targetElement.TryFindResource(styleName);

        if (newStyle == null)
            newStyle = (Style)targetElement.TryFindResource("MyDefaultStyleName");

        return newStyle;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这不是我经常做的事情,从记忆中工作:)

It's not something I do very often, but that should work from memory :)

这篇关于绑定WPF样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆