如何通过一个静态值在XAML来的IValueConverter [英] How to pass a static value to IValueConverter in XAML

查看:92
本文介绍了如何通过一个静态值在XAML来的IValueConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如文本看起来是这样的:

  NAME =M43; 
含量=这是要显示的文字;



然后,我会想将名字传下去的文字(即标识符)到<$ C 。$ C>的IValueConverter ,然后将查找名称和返回文本



我想转换器是这个样子:

 公共类StaticTextConverter:的IValueConverter 
{
公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
{
如果(值= NULL)
{
返回App.StaticTexts.Items.SingleOrDefault(T =>!t.Name.Equals(值) )。内容;
}

返回NULL;
}
}



然后在XAML:

 <电话:PhoneApplicationPage.Resources> 
<助手:StaticTextConverter X:NAME =StaticTextConverter/>
< /电话:PhoneApplicationPage.Resources>



< TextBlock的文本={结合'M43',转换器= {StaticResource的StaticTextConverter}}/>



不过,这似乎并没有工作,我不知道我在值传递给变频器正常。



有没有人有一些建议吗?


解决方案

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

要总结了使用该解决方案的的IValueConverter 我要绑定我在下面的庄园控制:

 <电话:PhoneApplicationPage.Resources> 
<助手:StaticTextConverter X:NAME =TextConverter/>
< /电话:PhoneApplicationPage.Resources>

< TextBlock的文本={结合转换器= {StaticResource的TextConverter},ConverterParameter = M62}/>



由于文本的ID与转换器参数传递中,转换器看起来几乎是相同的:

 公共类StaticTextConverter:的IValueConverter 
{
公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
{
如果(参数= NULL&放大器;!&放大器;参数是字符串)
{
返回App.StaticTexts.Items.SingleOrDefault(T =>吨。.Name.Equals(参数))内容;
}

返回NULL;
}
}



然而,事实证明,绑定,从而如果这样做的转换器不调用的的有的DataContext 。为了解决这个问题,控制的的DataContext 属性只是必须设置的东西随意:

 < TextBlock中的DataContext =任意
文本={结合转换器= {StaticResource的TextConverter},ConverterParameter = M62}/>



然后一切按预期工作!


I would like to use static texts fetched from a web service in my WP7 app. Each text has a Name (the indetifier) and a Content property.

For example a text could look like this:

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

I would then like to pass the Name (i.e. the identifier) of the text to an IValueConverter, which would then look up the the Name and return the text.

I figured the converter to look something like this:

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

Then in the XAML:

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

...

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

However, this does not seem to work and I am not sure that I pass in the value to the converter correctly.

Does anyone have some suggestions?

解决方案

I finally found the answer. The answer was a mix between that of @Shawn Kendrot and another question I asked here: IValueConverter not getting invoked in some scenarios

To summarize the solution for using the IValueConverter I have to bind my control in the following manor:

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

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

Since the ID of the text is passed in with the converter parameter, the converter looks almost the same:

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

However, as it turns out, the binding and thus the converter is not invoked if it does not have a DataContext. To solve this, the DataContext property of the control just has to be set to something arbitrary:

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

And then everything works as intended!

这篇关于如何通过一个静态值在XAML来的IValueConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆