Silverlight自定义按钮内容

时间:2011-06-23 13:58:27

标签: silverlight silverlight-4.0

在SL 4应用程序中配置每个客户端按钮的文本(内容)的好方法是什么?我仍然是SL的新手,所以这看起来似乎微不足道。

这个问题并不新鲜。系统当前具有ButtonA内容的静态XAML属性为“Do Stuff”(Content =“DoStuff”)。现在有一个客户希望阅读“做事”。在整个系统的任意位置,这种情况有时会继续出现。

我有一个包含自定义文本的字典,但是喜欢(如果可能的话)能够有一个默认值,只有覆盖如果有一个字典条目。

从概念上讲,能够拥有:

会很高兴
<Button Content="Do Stuff" OverrideContentKey="ButtonAOverrideContent" />

如果字典中有一个ButtonAOverrideContent键,它将覆盖它,否则将显示“Do Stuff”。

是否有办法编写转换器并在App.xaml中创建一些条目,然后允许所有按钮有条件地覆盖内容?我所看到的转换器看起来似乎没有一种平滑的方法可以将有关控件的信息(例如覆盖键)传递给它们。

1 个答案:

答案 0 :(得分:2)

您可以使用ConverterParameter的{​​{1}}属性将覆盖内容密钥传递给转换器。

Binding

在App.Xaml资源中: -

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

然后在按钮上: -

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