John Spiegel
John Spiegel

Reputation: 1903

Silverlight Custom Button content

What would be a good way to have the text (content) of buttons per-client configurable in a SL 4 app? I'm still pretty novice w/ SL so this may seem trivial.

The issue isn't new. The system currently has a static XAML attribute for ButtonA's content as "Do Stuff" (Content="DoStuff"). Now one client wants that to read "Do Things". This will continue to crop up on occasion in arbitrary places across the system.

I have a dictionary available that will contain the custom text, but would LIKE (if possible) to be able to have a default value and only override if there is a dictionary entry.

Conceptually, it would be nice to be able to have:

<Button Content="Do Stuff" OverrideContentKey="ButtonAOverrideContent" />

where if the dictionary has a key of ButtonAOverrideContent, it will override it, but otherwise "Do Stuff" will show.

Is there a way to perhaps write a converter and make some entries in App.xaml that would then allow all buttons to conditionally override the content? What I've seen of converters looks like there's not a smooth way to pass information about the control (e.g. the override key) to them.

Upvotes: 1

Views: 225

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189555

You can use a ConverterParameter property of a Binding to pass your override content key to a converter.

public class ReplaceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string key = (string)parameter;
        var someDictionary = GetYourReplacementDictionary();
        if (someDictionary.ContainsKey(key))
        {
            return someDictionary[key];
        }
        else
        {
            return value;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

In your App.Xaml resources:-

 <local:ReplaceConverter x:Key="replacer" />

Then on a button:-

 <Button Content="{Binding Source='Do Stuff', ConverterParameter=ButtonAOverrideContent, Converter={StaticResource replacer}}" />

Upvotes: 2

Related Questions