Is there any way to add * to xaml? I am looking ...">

Add text to static resource

If I have a label:

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

Is there any way to add * to xaml?

I am looking for something like:

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

I host the contents of my controls from the resource dictionary because the application supports multiple languages. I was wondering if I can add * to xaml so that I do not need to create an event and then add it when this event fires.

Edit:

In the resource dictionary, I have:

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

in my window I have: (I merge the last dictionary)

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

and displays the name

I like the name * to appear on the label, not just the name

Perhaps this will be possible to achieve with style.

+5
source share
2 answers

There are several ways to do this:

  • ContentStringFormat:

    <Label Content="{StaticResource Foo}" ContentStringFormat='{}{0}*'/>
    
  • StringFormat ( , TextBlock Label )

    <Label>
       <TextBlock 
           Text="{Binding Source={StaticResource Foo}, StringFormat='{}{0}*'}"/>
    </Label>
    
  • converter, *
+12

@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 resouce

<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} }" />
+1

All Articles