【问题标题】:WPF converter for labels' content标签内容的 WPF 转换器
【发布时间】:2020-07-13 10:35:30
【问题描述】:

我正在尝试覆盖标签的输出,假设它包含“帐户”,并且客户希望将帐户呈现为“成员”(因此将其视为本地化转换器?)

我的问题;这可能与“硬编码”内容有关吗?或者我必须创建一个包含所有标签内容的静态文件(当然是 iNotifiyStatic)? *用于绑定?

xaml:

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

资源文件:包括所有尝试,来自多个来源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;
    }
}

【问题讨论】:

    标签: wpf data-binding converters wpf-style


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 2022-12-08
      相关资源
      最近更新 更多