从后面的代码更新ComboBox SelectedItem [英] Updating a ComboBox SelectedItem from code behind

查看:54
本文介绍了从后面的代码更新ComboBox SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ComboBox的View绑定到我的viewModel属性. 一切正常,但我实际上想重用View,并且需要 用给定值更新控件.设置属性不会更新视觉UI 即使事件被解雇,一切看起来也不错.

im having a View with a ComboBox bound to my viewModel property. Everything works fine but i actually want to reuse my View and need to update the controls with a given value. Setting the property wont update the visual UI even to event is fired and everyting looks good.

所有作品都接受ComboBox视觉UI.

Everything works accept the ComboBox visual UI.

提示?!

XAML控件

<telerik:RadComboBox 
            ItemTemplate="{StaticResource SelectUserComboBoxTemplate}"
            SelectedItem="{Binding Path=SelectedUser, Mode=TwoWay,
            UpdateSourceTrigger=PropertyChanged}" 
            ItemsSource="{Binding Path=C_users}" 
            telerik:TextSearch.TextPath="displayName"
            Name="radComboBox1" 
            Margin="14,12,0,0" 
            Height="31" 
            VerticalAlignment="Top" 
            HorizontalAlignment="Left" 
            Width="253" 
            TextSearchMode="Contains"
            IsEditable="True"
            OpenDropDownOnFocus="True" 
            IsFilteringEnabled="True"
            >
    </telerik:RadComboBox>

设置值的重载构造函数

    public TicketControlTabViewModel(ticket t)
    {
        activeTicket = t;
        SelectedUser = customerServiceClient.getUser(t.customer_users.id);
        MetaString = t.meta;
        Description = t.description;
        ActiveId = t.id.ToString();
        Selected_priority = t.priority;
        SelectedStatus = t.status;
        this.RefreshC_users();
        this.RefreshSupportDepartments();
        this.RefreshSupportUsers();
    }

我的ViewModel中的属性

The property in my ViewModel

    private customer_users selectedUser { get; set; }
    public customer_users SelectedUser
    {

        get {
            return this.selectedUser;
            }
        set {
              if (value != null){
              this.selectedUser = value;
              this.UpdateCustomerDepartment(value);
              this.OnPropertyChanged("SelectedUser");
              SaveTicket();
              }

            }
    }

推荐答案

默认情况下,WPF通过引用而不是通过值比较SelectedItem.这意味着,如果SelectedItemItemsSource中的项目不是完全相同的内存对象,则比较将返回false,并且该项目将不会被选择.

By default, WPF compares the SelectedItem by reference, not by value. That means if the SelectedItem isn't the exact same object in memory as the item in your ItemsSource, then the comparisom will return false and the item will not get selected.

例如,这可能行不通

MyCollection = new ObservableCollection<User>(DAL.GetUsers());
SelectedUser = DAL.GetUser(1);

无论如何,

MyCollection = new ObservableCollection<User>(DAL.GetUsers());
SelectedUser = MyCollection.FirstOrDefault(p => p.Id == 1);

这是因为第二个示例将SelectedUser设置为MyCollection中实际存在的项目,而第一个示例则可能没有.即使数据相同,它们也会引用内存中的不同对象.

That's because the 2nd example sets the SelectedUser to an item that actually exists in MyCollection, while the 1st example might not. Even if the data is the same, they reference different objects in memory.

如果您选择的项目未引用与ItemsSource项目在内存中相同的项目,则使用SelectedValueSelectedValuePath绑定ComboBox的默认选择,或覆盖类的.Equals()方法以返回如果要比较的对象中的数据相同,则为true.

If your selected item doesn't reference the same item in memory as your ItemsSource item, then either use SelectedValue and SelectedValuePath to bind your ComboBox's default selection, or overwrite the .Equals() method of your class to return true if the data in the objects being compared is the same.

public override bool Equals(object obj)
{
    if (obj == null || !(obj == MyClass))
        return false; 

    return ((MyClass)obj).Id == this.Id);
}

这篇关于从后面的代码更新ComboBox SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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