WPF从ComboBox获得不同的值

时间:2016-06-16 10:38:58

标签: c# wpf combobox

我的ComboBox看起来像这样:

<ComboBox Name="cbMDLName" Style="{StaticResource ComoboBox}"     
DisplayMemberPath="Name" ItemsSource="{Binding Path=MDLCollection}" 
SelectionChanged="cbMDLName_SelectionChanged" Grid.Row="0" Grid.Column="1"/>

DataContext是从代码隐藏文件中设置的。 this.DataContext = this.ManagerRoot.MDLM;

ComboBox的ItemSource设置为ObservableCollection<MDL> MDLCollection {get;set;}

MDL.cs

public class MDL : INotifyPropertyChanged
{
    public AdditionManager AdditionManager { get; set; }

    private string _name;
    public string Name
    {
        get { return this._name; }
        set 
        { 
            this._name = value;
            this.OnPropertyChanged("Name");
        }
    }

    private int _index;
    public int Index
    {
        get { return this._index; }
        set 
        { 
            this._index = value;
        }
    }
}

现在我想从所选条目中获取索引。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

你可以试试这个。

int selectedValue = (int)cbMDLName.SelectedValue;