从转换器返回动态资源
我想根据布尔值的状态(在本例中为复选框的状态)更改 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”有多个定义,具体取决于设置白天模式或夜间模式。这些事件不会触发警告颜色发生更改。 有没有办法使返回值动态化或者我需要重新考虑我的设计?
I want to change the color of a WPF control depending on the state of a bool, in this case the state of a checkbox.
This works fine as long as I'm working with StaticResources:
My control
<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>
My converter:
[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;
}
}
The problem is that I have several definitions of the Resource "WarningColor" dependant on setting day mode or night mode. These events does not trig the WarningColor to change.
Is there a way to make the return value dynamic or do I need to rethink my design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法从转换器返回动态内容,但如果您唯一的条件是布尔值,您可以使用
Triggers
轻松地将整个转换器替换为Style
:例如,
如果现在资源具有该键已更改,背景也应更改。
You cannot return something dynamic from a converter, but if your only condition is a bool you can easily replace the whole converter with a
Style
usingTriggers
:e.g.
If now the resource with that key is changed the background should change as well.
返回动态资源引用的方法非常简单,使用 DynamicResourceExtension 构造函数并为其提供资源键。
用法:
Provider 类的定义应包含键:
并且该键的值将在资源字典中声明:
这样,转换器将根据提供的资源键动态地将 DynamicResource 分配给绑定属性。
The way to return a dynamic resource reference is pretty simple using a DynamicResourceExtension constructor and supplying it a resource key.
Usage:
Definition of the Provider class should contains the key:
And the value for the key would be declared in the resource dictionary:
This way, the converter would dynamically assign a DynamicResource to the bound property depending on the resource key supplied.