在Windows Phone的多值转换器 [英] Multivalue converter in windows phone

查看:130
本文介绍了在Windows Phone的多值转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows Phone应用程序。

I have a windows phone application.

让我们说我有一个公开的客户列表中CustomersViewModel类。我在XAML结合到该列表列表:

Lets's say I have a CustomersViewModel class that exposes the list of customers. I have a list in xaml that binds to that list:

<ListBox ItemsSource="{Binding Path=Data}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Text="{Binding Converter={StaticResource userIdToNameConverter}" />                    
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>



因此,在列表框中的每个项目将绑定到一个单一的客户对象。

So each item in the list box will bind to a single customer object.

CustomersViewModel还有一个附加属性。

CustomersViewModel has an additional property

string StoreId

在我上面的XAML,我想给STOREID传递到转换器,除了给客户对象,我今年已经流逝。怎么可以这样做优雅?

In my XAML from above, I would like to pass the StoreId to the converter, in addition to the customer object that I am already passing. How can this be done elegantly?

看来IMultiValueConverter上不存在WP8,这是不可能做数据绑定到转换器的ConverterParameter。

It seems IMultiValueConverter does not exist on WP8, and it is not possible to do data binding to ConverterParameter of the converter.

推荐答案

此的博客文章围绕解释了工作的问题。这个想法是创建一个依赖属性你的转换器。然后,您可以绑定你的 STOREID 来此的,而不是使用 ConverterParameter

This blog post explains a work around for the problem. The idea is to create a dependency property on your converter. You can then bind your StoreId to this, instead of using the ConverterParameter.

所以,在你的 UserIdToNameConverter ,你需要从的DependencyObject 继承,并添加依赖属性:

So on your UserIdToNameConverter, you need to inherit from DependencyObject, and add the dependency property:

public class UserIdToNameConverter : DependencyObject, IValueConverter
{
    public string StoreId
    {
        get { return (string) GetValue(StoreIdProperty); }
        set { SetValue(StoreIdProperty, value); }
    }

    public static readonly DependencyProperty StoreIdProperty =
        DependencyProperty.Register("StoreId",
                                    typeof (string),
                                    typeof (UserIdToNameConverter),
                                    new PropertyMetadata(string.Empty));

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        //Your current code
        //Can now use StoreId instead of ConverterParameter
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        //Same as above;
    }
}

您就可以绑定到内视图的这种依赖属性资源:

You can then bind to this dependency property within your view's resources:

<UserControl.Resources>
    <UserIdToNameConverter x:Key="UserIdToNameConverter" StoreId="{Binding StoreId}"/>
</UserControl.Resources>

这是假设你的视图的的DataContext 设置在 CustomersViewModel 在那里可以找到 STOREID 属性。然后,您可以使用转换器相同的方式,你在你的问题都有。

This is assuming your view's DataContext is set to the CustomersViewModel where it can find the StoreId property. You can then use the converter the same way as you have in your question.

作为一个侧面说明,它不会,如果你创建中的转换器<$工作C $ C>的ItemTemplate 而不是内部资源。请参阅博客文章以了解更多信息。一切归功于该博客的作者,塞巴斯蒂安Pertus。

As a side note, it won't work if you create the converter within the ItemTemplate instead of inside the Resources. See the blog post for more information. All credit goes to the blog's author, Sebastien Pertus.

这篇关于在Windows Phone的多值转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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