WPF转换器,用于标签内容 [英] WPF converter for labels' content

查看:121
本文介绍了WPF转换器,用于标签内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试覆盖标签的输出,说标签包含"Account",并且客户希望将帐户呈现为"Member"(这样想像是本地化转换器吗?)

I'm trying to override the output of a label, say it contained "Account" and a client wants account rendered as "Member" (so kind of think of this as a localisation converter?)

我的问题; 硬编码"内容有可能吗?还是必须创建一个包含所有标签内容的静态文件(当然要使用iNotifiyStatic)? *要绑定吗?

My Question; is this possible with "hardcoded" content? or MUST i create a static file containing all label content (with iNotifiyStatic of course)? *for binding?

xaml:

 <Label Style="{StaticResource LabelLeft}" Content="Account Name:"></Label>

资源文件:包括来自多个来源的所有尝试

Resource File: Including all attempts made, from multiple sources heres the most meaningful one.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:converters ="clr-namespace:Company.Resources.Converters">   

<converters:LocationLabelsConverter x:Key="LocationLabelsConverter" />

<Style x:Key="LabelLeft"  TargetType="{x:Type Label}" >
    <Setter Property="Margin" Value="10 0 0 0"></Setter>
    <Setter Property="Height" Value="22"></Setter>
    <Setter Property="Padding" Value="0 0 0 0"></Setter>
    <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
    <!-- Att1 -->
    <!--<Setter Property="TextBlock.Text" Value="{Binding RelativeSource={RelativeSource self},
                                   Path=Content,
                                   Converter={StaticResource LocationLabelsConverter}}"></Setter>-->
    <!-- Att2 -->
    <!--<Setter Property="Content">
        <Setter.Value>
            <Binding Path="Content" RelativeSource="{RelativeSource self}">
                <Binding.Converter>
                    <converters:LocationLabelsConverter/>
                </Binding.Converter>
            </Binding>
        </Setter.Value>  
    </Setter>-->
    <!-- Att3 -->
    <!--<Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource self},
                                   Path=Content,
                                   Converter={StaticResource LocationLabelsConverter}}">
            <Setter Property="Content" Value="Test123"/>
        </DataTrigger>
    </Style.Triggers>-->        
</Style>

这是转换器:

[ValueConversion(typeof(string), typeof(string))]
public sealed class LocationLabelsConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            return "hello sweety";// (string)value; //The goal here in the end is to run it through a method to replace string with correct text.
        }
        else return null;
    }        

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

推荐答案

您可以像这样应用转换器:

you can apply converter like this:

<Label Content="{Binding Source='Account Name:', Converter={StaticResource LocationLabelsConverter}"/>

这篇关于WPF转换器,用于标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆