Silverlight ComboBox绑定与值转换器 [英] Silverlight ComboBox binding with value converter

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

问题描述

我正在尝试通过使用组合键作为键来转换组合框的显示值,以查找想要显示的值.我似乎无法使其正常工作.

I'm attempting to convert the displayed value of a combobox by using its binding as a key to look for the value I would like to display. I can't seem to get it to work.

我的用户控件的数据上下文是MyObject. MyObject具有属性"MasterDrawerId",它是"MyReferencedObject"的ID.

The datacontext of my user control is MyObject. MyObject has a property "MasterDrawerId", which is the Id of "MyReferencedObject".

在应用程序的其他地方,可通过App.xaml.cs的静态属性访问的是"MyOtherObjects"的集合. "MyReferencedObject"与"MyOtherObject"的ID具有外键关系.

Elsewhere in my application, accessible through a static property of my App.xaml.cs is a collection of "MyOtherObjects". "MyReferencedObject" has a foreign key relationship with the Id of "MyOtherObject".

我的组合框绑定到"MasterDrawerId",这是传递给转换器的内容. 然后,我将其用作"MyReferencedObject"的查询,以获取"MyOtherObject"的外键ID,以显示该对象的名称.

My combobox is bound to the "MasterDrawerId", which is what's passed into the converter. I then use that as a lookup for "MyReferencedObject" to get the foreign key Id of "MyOtherObject" in order to display the name of that object.

我知道这似乎令人困惑,但它基本上只是使用datacontext的属性来进行查找并在组合框中显示其位置的另一个对象的名称.

I know it seems confusing but it's basically just using the property of the datacontext in order to do a lookup and display the name of another object in its place within a combobox.

这是我的代码:

masterSiteComboBox.DisplayMemberPath = "Name";
Binding binding = new Binding("MasterDrawerId");
binding.Mode = BindingMode.TwoWay;
binding.Converter = new DrwIdToSiteConverter();
masterSiteComboBox.SelectedItem = binding;
masterSiteComboBox.ItemsSource = ListOfMyOtherObjects;

这是我的转换器代码:

公共类DrwIdToSiteConverter:IValueConverter { 公共DrwIdToSiteConverter() { }

public class DrwIdToSiteConverter : IValueConverter { public DrwIdToSiteConverter() { }

    public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        XferSite site = new XferSite();

        foreach(XferUserDrawerPermissions perm in App.UserDrawerPermissions)
        {
            if (perm.DocumentTypeId.Match(value.ToString()))
            {
                site.Id = int.Parse(perm.SiteId);
                site.Name = perm.SiteName;
                break;
            }
        }

        return site;
    }

    public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

我在转换器的转换"方法的第一行设置了一个断点,但它从未被命中.

I set a breakpoint at the first line of my "Convert" method of my converter and it never gets hit.

推荐答案

代替

masterSiteComboBox.SelectedItem = binding;

masterSiteComboBox.SetBinding(ComboBox.SelectedItemProperty, binding);

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

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