繁体   English   中英

如何在XAML中将静态值传递给IValueConverter

[英]How to pass a static value to IValueConverter in XAML

我想在我的WP7应用程序中使用从Web服务获取的静态文本。 每个文本都有一个名称(indetifier)和一个Content属性。

例如,文本可能如下所示:

Name = "M43";
Content = "This is the text to be shown";

然后,我想将文本的名称(即标识符)传递给IValueConverter ,然后IValueConverter将查找Name并返回文本。

我认为转换器看起来像这样:

public class StaticTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            return App.StaticTexts.Items.SingleOrDefault(t => t.Name.Equals(value)).Content;
        }

        return null;
    }
}

然后在XAML中:

<phone:PhoneApplicationPage.Resources>
    <Helpers:StaticTextConverter x:Name="StaticTextConverter" />
</phone:PhoneApplicationPage.Resources>

...

<TextBlock Text="{Binding 'M43', Converter={StaticResource StaticTextConverter}}"/>

但是,这似乎不起作用,我不确定我是否正确地将值传递给转换器。

有人有什么建议吗?

我终于找到了答案。 答案是@Shawn Kendrot和我在这里问的另一个问题之间的混合: IValueConverter在某些情况下没有被调用

总结使用IValueConverter的解决方案,我必须在以下庄园中绑定我的控件:

<phone:PhoneApplicationPage.Resources>
    <Helpers:StaticTextConverter x:Name="TextConverter" />
</phone:PhoneApplicationPage.Resources>

<TextBlock Text="{Binding Converter={StaticResource TextConverter}, ConverterParameter=M62}" />

由于使用转换器参数传入文本的ID,因此转换器看起来几乎相同:

public class StaticTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (parameter != null && parameter is string)
        {
            return App.StaticTexts.Items.SingleOrDefault(t => t.Name.Equals(parameter)).Content;
        }

        return null;
    }
}

然而,事实证明,装订,因此,如果没有一个转换器不调用DataContext 要解决这个问题,控件的DataContext属性必须设置为任意的:

<TextBlock DataContext="arbitrary" 
           Text="{Binding Converter={StaticResource TextConverter}, ConverterParameter=M62}" />

然后一切都按预期工作!

如果要使用值转换器,则需要将字符串传递给值转换器的参数

XAML:

<TextBlock Text="{Binding Converter={StaticResource StaticTextConverter}, ConverterParameter=M43}"/>

转换器:

public class StaticTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (parameter != null)
        {
            return App.StaticTexts.Items.SingleOrDefault(t => t.Name.Equals(parameter)).Content;
        }

        return null;
    }
}

问题在于你的绑定。 它将检查DataContext ,并在此对象上,它将尝试评估该对象上的属性M62ValueboxConsent

您可能希望在应用程序的某处添加静态密钥,您可以将其绑定到:

<TextBlock Text="{Binding Source="{x:Static M62.ValueboxConsent}", Converter={StaticResource StaticTextConverter}}" />

M62是一个静态类,其中的键位于..如下所示:

public static class M62
{
    public static string ValueboxConsent
    {
        get { return "myValueBoxConsentKey"; }
    }
}
xmlns:prop="clr-namespace:MyProj.Properties;assembly=namespace:MyProj"

<TextBlock Text="{Binding Source={x:Static prop:Resources.MyString}, Converter={StaticResource StringToUpperCaseConverter}}" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM