在组合框中显示的当前值 [英] Current value displayed in ComboBox

查看:86
本文介绍了在组合框中显示的当前值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当ComboBox中的项目是数据库中的自定义对象时,我似乎无法获得当前所选值显示在ComboBox中。我将以下测试ViewModel和View放在一起。 SimpleWidgets和ObjectWidgets的行为符合预期。 CodeWidget(从数据库中检索为自定义 Code对象的列表)从不显示SelectedCodeWidget。

I can't seem to get the current selected value to show in the ComboBox when the items in the ComboBox are custom objects from the database. I put together the following test ViewModel and View. SimpleWidgets and ObjectWidgets behave as expected. CodeWidgets (list retrieved from the database as custom 'Code' objects) never displays the SelectedCodeWidget.

    public class TestViewModel:BaseTabViewModel
    {
    private List<int> simpleWidgets;
    public List<int> SimpleWidgets
    {
        get { return simpleWidgets; }
        set
        {
            simpleWidgets = value;
            NotifyOfPropertyChange(() => SimpleWidgets);
        }
    }

    private int selectedSimpleWidget;
    public int SelectedSimpleWidget
    {
        get { return selectedSimpleWidget; }
        set
        {
            selectedSimpleWidget = value;
            NotifyOfPropertyChange(() => SelectedSimpleWidget);
        }
    }

    private List<Widget> objectWidgets;
    public List<Widget> ObjectWidgets
    {
        get { return objectWidgets; }
        set
        {
            objectWidgets = value;
            NotifyOfPropertyChange(() => ObjectWidgets);
        }
    }

    private Widget _selectedObjectWidget;
    public Widget SelectedObjectWidget
    {
        get { return _selectedObjectWidget; }
        set
        {
            _selectedObjectWidget = value;
            NotifyOfPropertyChange(() => SelectedObjectWidget);
        }
    }

    private List<ICode> codeWidgets;
    public List<ICode> CodeWidgets
    {
        get { return codeWidgets; }
        set
        {
            codeWidgets = value;
            NotifyOfPropertyChange(() => CodeWidgets);
        }
    }
    private Code _selectedCodeWidget;
    public Code SelectedCodeWidget
    {
        get { return _selectedCodeWidget; }
        set
        {
            _selectedCodeWidget = value;
            NotifyOfPropertyChange(() => SelectedCodeWidget);
        }
    }

    public TestViewModel()
    {
        DisplayName = "Test Data";
        IsEnabled = true;//control this with permissions
        //Simple int comboBox
        SimpleWidgets = new List<int> {1, 3, 5, 7, 9};
        SelectedSimpleWidget = 7;
        //Object filled ComboBox
        ObjectWidgets= new List<Widget>();
        Widget w = new Widget {Key = 2, Description = "test2"};
        ObjectWidgets.Add(w);
        w = new Widget { Key = 4, Description = "test4" };
        ObjectWidgets.Add(w);
        w = new Widget {Key = 6, Description = "test6"};
        ObjectWidgets.Add(w);
        SelectedObjectWidget = w;
        //Code filled ComboBox
        CodeWidgets = (new Code().Get("UPFLTY").Result).ToList();
        SelectedCodeWidget = new Code(2707);
    }
}

public class Widget
{
    public int Key { get; set; }
    public string Description { get; set; }
}

TestView.xaml是:

The TestView.xaml is:

<UserControl x:Class="CAB.DataManager.App.Views.TestView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UniformGrid>

        <ComboBox Name="SimpleWidgets"></ComboBox>
            <ComboBox Name="ObjectWidgets"
                     DisplayMemberPath="Description" >
             </ComboBox>
        <ComboBox x:Name="CodeWidgets">
                 <ComboBox.ItemTemplate>
                     <DataTemplate>
                        <StackPanel>
                            <TextBox Text="{Binding Description}"/>
                        </StackPanel>
                     </DataTemplate>
                 </ComboBox.ItemTemplate>
        </ComboBox>
    </UniformGrid>
</UserControl>

SimpleWidgets和ObjectWidgets显示所选值,并在下拉列表中显示所有值。 CodeWidgets为空白,但下拉列表中确实包含所有值。我缺少什么?

SimpleWidgets and ObjectWidgets show the selected value and have all values in the dropdown. CodeWidgets is blank but does have all values in the dropdown. What am I missing?

推荐答案


<$ c $中的 instance c> SelectedItem 必须存在于 之内。

The instance that is in SelectedItem must exist within ItemsSource.

从您的代码中

//Code filled ComboBox
CodeWidgets = (new Code().Get("UPFLTY").Result).ToList();
SelectedCodeWidget = new Code(2707); // THIS is where you bugged!

您可以看到 SelectedCodeWidget 设置为 Code 的新实例,不在 CodeWidgets 中的实例。

You can see that SelectedCodeWidget is set to a new instance of Code, one that is not in CodeWidgets.

答案仅是执行以下操作:

The answer is simply to do the following:

//Code filled ComboBox
CodeWidgets = (new Code().Get("UPFLTY").Result).ToList();
var selectedCode = FindCodeByDERP(CodeWidgets, 2707); // MAKE THIS METHOD
SelectedCodeWidget = selectedCode;

其中 FindCodeByDERP 是这样写的在 CodeWidgets 中搜索实例,并返回 2707 最难的实例。不管它是什么意思。我的意思是,您有一个代码 UPFLTY ,但是您创建了另一个传入 2707 的实例...这没有任何意义。是的。

Where FindCodeByDERP is a method you write that searches through the instances in CodeWidgets and returns the instance which 2707s the hardest. Whatever that means. I mean, you've got a Code UPFLTY yet you create another instance passing in 2707... that makes no sense. Yeah.

这篇关于在组合框中显示的当前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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