C# 是否可以在样式中使用转换器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/378979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to use a converter within a style?
提问by
Is it possible to use a converter within a style? For instance I am trying to create a styled TextBlock
whose text resizes based on the ActualHeight
property of the TextBlock
. The resizing would be done via a converter.
是否可以在样式中使用转换器?比如我想创建一个样式TextBlock
,其文本的大小调整基础上ActualHeight
的财产TextBlock
。调整大小将通过转换器完成。
采纳答案by Kent Boogaart
Yes, this is possible. For example:
是的,这是可能的。例如:
<Style TargetType="TextBlock">
<Setter Property="FontSize">
<Setter.Value>
<Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}">
<Binding.Converter>
<MyConverter/>
</Binding.Converter>
</Binding>
</Setter.Value>
</Setter>
</Style>
Depending on your exact scenario, you might also be able to use the more succinct:
根据您的具体情况,您也许还可以使用更简洁的:
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Converter={StaticResource MyConverter}}"/>
</Style>
回答by yannduran
I managed to get something similar to work by using:
我设法通过使用获得了类似于工作的东西:
<Setter Property="Text">
<Setter.Value>
<Binding Path="CompanyName">
<Binding.Converter>
<conv:UppercaseConverter/>
</Binding.Converter>
</Binding>
</Setter.Value>
</Setter>
Hope it works for you too.
希望它也适用于你。
Yann
雅恩
PS - CompanyName is the name of the actual ViewModel property I was binding the textblock to
PS - CompanyName 是我将文本块绑定到的实际 ViewModel 属性的名称