动态资源数据触发检查值
我有一个 WPF 应用程序,其内容应以德语和英语显示。因此,我创建了两个单独的资源字典,其中包含每种语言的字符串片段。应用程序运行时可以在语言之间切换。 此时我遇到了一个问题。用户可以进行一些设置。如果设置成功完成,则会显示一条消息。消息的文本取自资源字典。根据成功或错误消息,文本显示为绿色或红色。
<TextBlock Text="{Binding UpdateTaxPercentageSettingsMessage}" FontWeight="Bold">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{DynamicResource tax_percentage_update_went_wrong}">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource active_tax_law_update_went_wrong}">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource tax_percentage_was_updated_successfully}">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource active_tax_law_was_updated_successfully}">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource differential_taxation_info_update_went_wrong}">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource differential_taxation_info_was_updated_successfully}">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
为了能够在应用程序运行时切换语言,从资源字典中获取的字符串片段必须是DynamicResource
。不幸的是,我无法使用DynamicResource
作为数据触发器中的条件。有人遇到过类似的问题吗?我很感谢您的建议!
I have a WPF application and the content of should be displayed in German and English. Therefore, I created two seperate resource dictionaries which contain string snippets in each language. It is possible to switch between the languages while the application is running.
At this point I got stuck at a problem. There are some settings the user can make. If the setting was completed successfully a message shows up. The text of the message is taken from the resource dictionary. Based on a success or error message the text is displayed green or red.
<TextBlock Text="{Binding UpdateTaxPercentageSettingsMessage}" FontWeight="Bold">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{DynamicResource tax_percentage_update_went_wrong}">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource active_tax_law_update_went_wrong}">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource tax_percentage_was_updated_successfully}">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource active_tax_law_was_updated_successfully}">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource differential_taxation_info_update_went_wrong}">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding UpdateTaxPercentageSettingsMessage}" Value="{StaticResource differential_taxation_info_was_updated_successfully}">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
To make switching languages possible while the application is running, the string snippets taken from the resource dictionary has to be a DynamicResource
. Unfortunately, I can not use DynamicResource
s as condition in data triggers. Has anyone faced a similar problem yet? I am grateful for your suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要对本地化文本使用触发器,它也不能与
DynamicResouce
一起使用,因为Value
不是依赖属性,也不可读。相反,创建一个描述您的错误的枚举
。公开错误的另一个属性,并在必要时实现 INotifyPropertyChanged 。
使用属性而不是您的资源作为触发器的
Value
。奖励回合。由于您的数据触发器是重复的(它们经常设置相同的值),请考虑使用带有自定义转换器的
绑定
,该转换器简单地说,如果绑定值与任何给定的值,应用此设置器。在范围内的任何资源字典中创建转换器的实例。
更改数据触发器并将数组中的目标值作为转换器参数传递。
Do not use triggers on localized text, neither does it work with
DynamicResouce
, becauseValue
is not a dependency property, nor is it readable. Instead, create anenum
that describes your errors.Expose anothe property for the error and implement
INotifyPropertyChanged
if necessary.Use the property instead of your resources as
Value
of the triggers.Bonus round. As your data triggers are repetitive (they set the same values often), consider using a
Binding
with a custom converter that simply says, if the bound value matches any of the given values, apply this setter.Create an instance of the converter in any resource dictionary in scope.
Change the data triggers and pass the target values in an array as converter parameters.