如何为所有ComboBox创建Generic SelectedItem

时间:2014-07-31 11:01:05

标签: c# wpf visual-studio xaml mvvm

我是WPF的新手。

我试图让所有组合框的SelectedItem变得通用,因为我的表单中有很多ComboBox。

当我为每个ComboBox使用不同的SelectedItem时,它可以正常工作。

C#代码:

private DropDownModel _mySelectedItem_DeviceType;
    public DropDownModel MySelectedItem_DeviceType
    {
        get { return _mySelectedItem_DeviceType; }
        set
        {
            if (_mySelectedItem_DeviceType != value)
            {
                _mySelectedItem_DeviceType = value;

                OnNotifyPropertyChanged("MySelectedItem_DeviceType");
            }
        }
    }
 private DropDownModel _mySelectedItem_All;
    public DropDownModel MySelectedItem_All
    {
        get { return _mySelectedItem_All; }
        set
        {
            if (_mySelectedItem_All != value)
            {
                _mySelectedItem_All = value;

                OnNotifyPropertyChanged("MySelectedItem_All");
            }
        }
    }

    private DropDownModel _mySelectedItem_Status;
    public DropDownModel MySelectedItem_Status
    {
        get { return _mySelectedItem_Status; }
        set
        {
            if (_mySelectedItem_Status != value)
            {
                _mySelectedItem_Status = value;

                OnNotifyPropertyChanged("MySelectedItem_Status");
            }
        }
    }
public event PropertyChangedEventHandler PropertyChanged;

    private void OnNotifyPropertyChanged(string propertyName)
    {
        var propertyChangedEvent = PropertyChanged;  

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    } 
var myListDropDown = (from b in _entities.DeviceTypes
                                  select new DropDownModel
                                  {
                                      ID = b.ID,
                                      Name = b.Name
                                  }).Distinct().ToList();
            myListDropDown.Add(new DropDownModel
            {
                ID = -1,
                Name = "Select Device"
            });
            myListDropDown = myListDropDown.OrderBy(x => x.Name).ToList();

            //DisplayDeviceType = new ObservableCollection<DropDownModel>(myListDropDown);
            DisplayDeviceType = new ObservableCollection<DropDownModel>(myListDropDown);

            MySelectedItem_DeviceType = (from b in myListDropDown
                                               where b.ID == (singleDevice == null ? -1 : singleDevice.DeviceTypeID)
                                               select b).FirstOrDefault();
var allStatus = (from b in _entities.DeviceStatuses
                               select new DropDownModel
                               {
                                   ID = b.ID,
                                   Name = b.Status
                               }).Distinct().ToList();
            allStatus.Add(new DropDownModel
            {
                ID = -1,
                Name = "Select Status"
            });
            allStatus = allStatus.OrderBy(x => x.Name).ToList();

            Status = new ObservableCollection<DropDownModel>(allStatus);

            MySelectedItem_Status = (from b in allStatus
                                  where b.ID == (singleDevice == null ? -1 : singleDevice.StatusID)
                                  select b).FirstOrDefault();

XAML:

 <ComboBox Name="DeviceTypeComboBox"
                              Grid.Column="1"
                              HorizontalAlignment="Left"
                              VerticalAlignment="Top"
                              Width="143.917"
                              Grid.Row="1"
                              ItemsSource="{Binding DisplayDeviceType}"
                              SelectedItem="{Binding MySelectedItem_DeviceType}"
                              SelectedValue="ID"
                              SelectedValuePath="ID"
                              DisplayMemberPath="Name"

                              />
<ComboBox Name="StatusTypeComboBox"
                              HorizontalAlignment="Left"
                              VerticalAlignment="Top"
                              Width="143.917"
                              Grid.Column="4"
                              Grid.Row="1"
                              ItemsSource="{Binding Status}"
                              SelectedItem="{Binding MySelectedItem_Status}"
                              SelectedValue="ID"
                              SelectedValuePath="ID"
                              DisplayMemberPath="Name"
                              />

//工作正常

但是当我尝试替换MySelectedItem_Status&amp;我的XAML中MySelectedItem_DeviceType MySelectedItem_All。它引发了一个异常。

谢谢,

1 个答案:

答案 0 :(得分:0)

听起来你需要抽象一点,并且有一个类包装了你试图通过组合框选择项设置的所有这些属性。

例如

public class DeviceInfo
{
  private DeviceDisplayType deviceType;
  public DeviceDisplayType DeviceType
  {
    get { return deviceType; }
    set { deviceType = value; }
  }

  private DeviceStatus deviceStatus
  public DeviceStatus DeviceStatus
  {
    get { return deviceStatus; }
    set { deviceStatus = value; }
  }
}

然后在viewmodel中拥有其中一个DeviceInfo个对象,并使xaml直接绑定到这些属性。

<ComboBox SelectedItem={Binding MyDeviceInfo.DeviceType} />

您仍然需要某个地方的属性,但您不是在不同的VM属性范围内,而是认识到这些属性都是更大的内聚类的一部分。