【问题标题】:Return a dynamic resource from a converter从转换器返回动态资源
【发布时间】:2011-09-30 11:56:26
【问题描述】:

我想根据布尔的状态更改 WPF 控件的颜色,在本例中是复选框的状态。 只要我使用 StaticResources,它就可以正常工作:

我的控制

<TextBox Name="WarnStatusBox" TextWrapping="Wrap" Style="{DynamicResource StatusTextBox}" Width="72" Height="50" Background="{Binding ElementName=WarnStatusSource, Path=IsChecked, Converter={StaticResource BoolToWarningConverter}, ConverterParameter={RelativeSource self}}">Status</TextBox>

我的转换器:

[ValueConversion(typeof(bool), typeof(Brush))]

public class BoolToWarningConverter : IValueConverter
{
    public FrameworkElement FrameElem = new FrameworkElement();

    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {                      
        bool state = (bool)value;
        try
        {              
            if (state == true)
                return (FrameElem.TryFindResource("WarningColor") as Brush);
            else
                return (Brushes.Transparent);
        }

        catch (ResourceReferenceKeyNotFoundException)
        {
            return new SolidColorBrush(Colors.LightGray);
        }
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return null;
    }
}

问题是我有几个依赖于设置白天模式或夜间模式的资源“WarningColor”定义。这些事件不会触发 WarningColor 更改。 有没有办法让返回值动态化,还是我需要重新考虑我的设计?

【问题讨论】:

  • 您能将 IMultiValueConverter 与 MultiBinding 一起使用吗?在我看来,您有 2 个值应该触发更改,而不仅仅是 IsChecked。
  • 是的。那是我最终做的。我必须添加一个处理程序来捕获显示更新事件并更新一个可观察的计数器。

标签: wpf ivalueconverter dynamicresource


【解决方案1】:

您不能从转换器返回动态的东西,但如果您的唯一条件是布尔值,您可以使用 Triggers 轻松地将整个转换器替换为 Style

例如

<Style TargetType="TextBox">
    <Setter Property="Background" Value="Transparent" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsChecked, ElementName=WarnStatusSource}" Value="True">
            <Setter Property="Background" Value="{DynamicResource WarningColor}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

如果现在更改了具有该键的资源,则背景也应该更改。

【讨论】:

    【解决方案2】:

    返回动态资源引用的方法非常简单,使用 DynamicResourceExtension 构造函数并为其提供资源键。

    用法:

    return new DynamicResourceExtension(Provider.ForegroundBrush);
    

    Provider类的定义应该包含key:

    public static ResourceKey ForegroundBrush 
    { 
        get 
        { 
            return new ComponentResourceKey(typeof(Provider), "ForegroundBrush"); 
        } 
    }
    

    并且键的值将在资源字典中声明:

    <ResourceDictionary
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:theme="clr-namespace:Settings.Appearance;assembly=AppearanceSettingsProvider">
    
    <Color x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type theme:Provider}, ResourceId=ForegroundColor}">#FF0000FF</Color>
    
    <SolidColorBrush x:Key="{ComponentResourceKey {x:Type theme:Provider}, ForegroundBrush}" Color="{DynamicResource {ComponentResourceKey {x:Type theme:Provider}, ForegroundColor}}" />
    </ResourceDictionary>
    

    这样,转换器将根据提供的资源键动态地将 DynamicResource 分配给绑定的属性。

    【讨论】:

    • 另外,不需要使用 ComponentResource。您可以使用纯字符串键定义 DynamicResource。
    猜你喜欢
    • 1970-01-01
    • 2023-01-27
    • 2019-04-28
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 2013-05-13
    相关资源
    最近更新 更多