Append text to static resource
Asked Answered
T

2

6

If I have the label:

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

Is there a way of appending the * in xaml?

I am looking for something like:

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

I am placing the content of my controls from a resource dictionary because the application supports multiple languages. I was wondering if I could append the * in xaml so that I do not have to create an event and then append it when that event fires.

Edit:

In a 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 that displays Name

I will like the label to display Name* not just Name

Maybe it will be possible to achieve that with a style.

Toreutic answered 16/4, 2012 at 20:46 Comment(0)
L
15

There are multiple ways to do it:

  1. With ContentStringFormat:

    <Label Content="{StaticResource Foo}" ContentStringFormat='{}{0}*'/>
    
  2. With Binding with StringFormat (it only work on string properies thats why you need to use a TextBlock as the Label's content)

    <Label>
       <TextBlock 
           Text="{Binding Source={StaticResource Foo}, StringFormat='{}{0}*'}"/>
    </Label>
    
  3. Or you can write a converter to append the *
Ligation answered 16/4, 2012 at 21:30 Comment(3)
Thanks that is useful to know. That does not append the * though. I guess I might need the converter. I will read about it and try it out.Toreutic
Can you elaborate on the "does not append the *" part? Because I've tested both solution in a sample app and they work. Can you also try it out in a new project (WPF .NET 4.0), because maybe there is something else in your project which causes problems here.Ligation
I was able to make it work with the converter. I don't know why the string format did not work. maybe because I load the dictionary at run time because the application supports multiple languages. Anyways I will post the working solution that I got thanks to your answer. Thanks!Toreutic
T
1

thanks to @nemesv answer this is what I ended up with:

I created the following converter:

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

On my app.xaml file I created the 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>

then anywhere in my application I will be able to use that converter as:

    <Label Content="{Binding Source={StaticResource NameField}, Converter={StaticResource RequiredFieldConverter} }" />
Toreutic answered 16/4, 2012 at 22:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.