简体   繁体   English

XAML - 样式中的转换器

[英]XAML - Converter within Style

I would like to include a Converter for TextBlock' Text property within a Style.我想在样式中包含 TextBlock' Text 属性的转换器。 I have the following code:我有以下代码:

<Page.Resources>
    <converters:StringFormatConverter x:Key="StringFormatConverter" />
    <Style x:Key="StringFormatStyle" TargetType="TextBlock">
        <Setter Property="Text">
            <Setter.Value>
                <Binding>
                    <Binding.Converter>
                        <converters:StringFormatConverter />
                    </Binding.Converter>
                </Binding>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>
...
<TextBlock Text="some text" Style="{StaticResource StringFormatStyle}" />
<TextBlock Text="{Binding AppName}" Style="{StaticResource StringFormatStyle}" />

The problem is that the Convert method of my Converter isn't being called.问题是我的 Converter 的Convert方法没有被调用。 The style is applied (100% sure of that).样式已应用(100% 确定)。

What could be the problem of the converter not being applied to the TextBlock' Text property?转换器未应用于 TextBlock 的 Text 属性可能是什么问题?

Long story short: I have several double properties in my ViewModel and I would like to display them formatted using a Converter.长话短说:我的 ViewModel 中有几个double属性,我想使用 Converter 显示它们的格式。

PS: My StringFormatConverter looks like this: PS:我的StringFormatConverter看起来像这样:

public class StringFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value == null)
        {
            return null;
        }

        if (parameter == null)
        {
            return value;
        }

        return string.Format((string)parameter, value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

Well, you can't.嗯,你不能。 In style you set text to some invalid binding with only a converter.在样式中,您仅使用转换器将文本设置为某些无效绑定。 And later you set it to static text.稍后您将其设置为静态文本。

If you want to add specific handling to a value and want to avoid code duplicates, use attached variable.如果要向值添加特定处理并避免代码重复,请使用附加变量。

Also note that you're creating two converters actually.另请注意,您实际上正在创建两个转换器。 One in style and one in resources.一种是风格,一种是资源。 So just reference the one from resources, don't need to create it in style again.所以只需从资源中引用一个,不需要再次创建它。

Here's attached property sample.这是附加的属性示例。

public class TextConv: DependencyObject
{
    public static readonly DependencyProperty TextProperty = 
    DependencyProperty.RegisterAttached(
      "Text",
      typeof(string),
      typeof(TextConv),
      new PropertyMetadata(null, OnValueChanged)
    );
    public static void SetText(UIElement element, string value)
    {
        element.SetValue(TextProperty, value);
    }
    public static string GetText(UIElement element)
    {
        return (string)element.GetValue(TextProperty);
    }
    private static void OnValueChanged(DependencyObject obj,  DependencyPropertyChangedEventArgs args)
   {
       obj.SetValue(TextBlock.TextProperty, args.NewValue.ToString() + "Hello!");
   }
}

And in xaml:在 xaml 中:

<TextBlock local:TextConv.Text="some text" />

It might not work out of the box but you get the general idea.它可能无法开箱即用,但您可以大致了解。

If you set the Text property explicitly, that will override the property setter in the style.如果您显式设置Text属性,则会覆盖样式中的属性设置器。

Use your converter in the Binding instead of the Style .Binding而不是Style使用您的转换器。

Text="{Binding AppName, Converter={StaticResource StringFormatConverterKey}}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM