【问题标题】:Append text to static resource将文本附加到静态资源
【发布时间】:2012-04-16 20:46:09
【问题描述】:

如果我有标签:

<Label Content="{StaticResource Foo}" />

有没有办法在 xaml 中附加 *?

我正在寻找类似的东西:

<Label Content="{StaticResource Foo, stringformat={0}*" />

我从资源字典中放置我的控件内容,因为该应用程序支持多种语言。我想知道是否可以在 xaml 中附加 *,这样我就不必创建一个事件,然后在该事件触发时附加它。

编辑:

在我的资源字典中:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:system="clr-namespace:System;assembly=mscorlib"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                >

     <system:String x:Key="Foo">Name</system:String>

 </ResourceDictionary>

在我的窗口中我有:(我合并了最后一个字典)

  <Label Content="{StaticResource 'Foo'}" />

并显示名称

我希望标签显示名称* 而不仅仅是名称

也许可以通过样式来实现。

【问题讨论】:

    标签: wpf xaml string-formatting staticresource


    【解决方案1】:

    有多种方法可以做到:

    1. 与ContentStringFormat:

      <Label Content="{StaticResource Foo}" ContentStringFormat='{}{0}*'/>
      
    2. 使用Binding with StringFormat(它只适用于字符串属性,这就是为什么您需要使用TextBlock 作为Label 的内容)

      <Label>
         <TextBlock 
             Text="{Binding Source={StaticResource Foo}, StringFormat='{}{0}*'}"/>
      </Label>
      
    3. 或者你可以写一个converter来附加*

    【讨论】:

    • 感谢您的了解。但这并没有附加 * 。我想我可能需要转换器。我会阅读并尝试一下。
    • 你能详细说明“不附加*”部分吗?因为我已经在示例应用程序中测试了这两种解决方案并且它们都有效。您是否也可以在新项目(WPF .NET 4.0)中尝试一下,因为您的项目中可能还有其他东西会导致这里出现问题。
    • 我能够让它与转换器一起工作。我不知道为什么字符串格式不起作用。可能是因为我在运行时加载了字典,因为应用程序支持多种语言。无论如何,由于您的回答,我将发布我得到的工作解决方案。谢谢!
    【解决方案2】:

    感谢@nemesv 回答这是我最终得到的结果:

    我创建了以下转换器:

    using System;
    using System.Windows.Data;
    using System.Globalization;
    
    namespace PDV.Converters
    {
        [ValueConversion(typeof(String), typeof(String))]
        public class RequiredFieldConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return value.ToString() + "*";
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var str = value.ToString();
                return str.Substring(0, str.Length - 2);
            }
        }
    }
    

    在我的 app.xaml 文件中,我创建了资源

    <Application x:Class="PDV.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml"
    
                 xmlns:conv="clr-namespace:PDV.Converters"  <!--  Include the namespace where converter is located-->
                 >
        <Application.Resources>
            <ResourceDictionary >
                <conv:RequiredFieldConverter x:Key="RequiredFieldConverter" />
            </ResourceDictionary>
        </Application.Resources>
    
    </Application>
    

    然后在我的应用程序中的任何地方我都可以使用该转换器:

        <Label Content="{Binding Source={StaticResource NameField}, Converter={StaticResource RequiredFieldConverter} }" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多