WPF Combobox绑定和SelectedValue与SelectedValuePath?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (1308)

这是我的数据库实例组合框的XAML。我在这里遇到的一个问题是“value”fpr SelectedValuePath,如果我将其更改为“DBInstanceName”则不起作用。

 <ComboBox x:Name="cbxRLFDBInstances" ItemsSource="{Binding DBInstances}" 
                  SelectedValue="{Binding SelectedDBInstance, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                  SelectedValuePath="value" DisplayMemberPath="DBInstanceName"/>

这是我的ViewModel代码:

namespace DatabaseTest.ViewModel
{

class RLFDatabaseTableViewModel : INotifyPropertyChanged
{
    Utilities dbtUtilities = new Utilities();


    public RelayCommand LoadDBInfoCommand
    {
        get;
        set;
    }


    public RLFDatabaseTableViewModel()
    {
        LoadDBInstances();

        LoadDBInfoCommand = new RelayCommand(LoadDBInfo);
    }


    public ObservableCollection<DBInstance> DBInstances
    {
        get;
        set;
    }


    public void LoadDBInstances()
    {
        ObservableCollection<DBInstance> dbInstances = new ObservableCollection<DBInstance>();
        DataTable dt = SmoApplication.EnumAvailableSqlServers(false);

        dbInstances.Add(new DBInstance { DBInstanceName = "fal-conversion\\mun2012ci" });
        dbInstances.Add(new DBInstance { DBInstanceName = "fal-conversion\\mun2014ci" });

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                dbInstances.Add(new DBInstance { DBInstanceName = dr["Name"].ToString() });
            }
        }

        DBInstances = dbInstances;

    }


    private DBInstance _selectedDBInstance;


    public DBInstance SelectedDBInstance
    {
        get
        {
            return _selectedDBInstance;
        }

        set
        {
            _selectedDBInstance = value;
            RaisePropertyChanged("SelectedDBInstance");
            //ClearComboBoxes();

        }
    }
}
}

这是我的模型代码。当我单步public string DBInstanceName执行此方法时,会多次执行。

namespace DatabaseTest.Model
{
public class RLFDatabaseTableModel { }


public class DBInstance : INotifyPropertyChanged
{
    private string strDBInstance;


    public override string ToString()
    {
        return strDBInstance;
    }


    public string DBInstanceName
    {
        get
        {
            return strDBInstance;
        }

        set
        {
            if (strDBInstance != value)
            {
                strDBInstance = value;
                RaisePropertyChanged("DBInstanceName");
            }
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}
提问于
用户回答回答于

你应该绑定SelectedItem属性ComboBoxSelectedDBInstance属性,摆脱SelectedValuePath

<ComboBox x:Name="cbxRLFDBInstances" ItemsSource="{Binding DBInstances}" 
              SelectedItem="{Binding SelectedDBInstance, UpdateSourceTrigger=PropertyChanged}" 
              DisplayMemberPath="DBInstanceName"/>

要最初选择项目,你应该将SelectedDBInstance属性设置为DBInstances集合中存在的项目:

public RLFDatabaseTableViewModel()
{
    LoadDBInstances();
    LoadDBInfoCommand = new RelayCommand(LoadDBInfo);
    SelectedDBInstance = DBInstances[0]; //selected the first item
}

扫码关注云+社区

领取腾讯云代金券