WPF converter for labels' content

谁都会走 提交于 2020-05-17 06:32:36

问题


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?)

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>

And here's the converter:

[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;
    }
}

回答1:


you can apply converter like this:

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


来源:https://stackoverflow.com/questions/60974926/wpf-converter-for-labels-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!