在ComboBox_SelectionChanged方法中获取ComboBox选定项

时间:2015-05-11 07:32:33

标签: c# wpf combobox

我正在使用C#和WPF跟踪MVVM模式。在我的.xaml.cs文件中,我有一个combobox_SelectionChanged方法,我需要访问ComboBox的所选项目。

var cb=sender as ComboBox;
Type t=cd.getType();

我尝试获取类型,然后绑定到SelectedItem属性的对象。那不成功。

然后我尝试了以下内容。

ComboBoxItem cbi = (ComboBoxItem)cb.SelectedItem;
string text = cbi.Content.ToString();

即便没有成功。有人可以建议我这样做吗?

2 个答案:

答案 0 :(得分:0)

Items属性的类型是您添加到ItemsSourceItemsSource集合的项目对象的类型。例如,如果您将SelectedItem属性绑定到字符串集合,var selectedItem = (string)cb.SelectedItem; 将返回其中一个字符串,如:

ln -s sourcepath linkpathname

答案 1 :(得分:0)

你绝对可以在不诉诸代码的情况下做到这一点。以下是一个示例:

将ViewModel公开为构造函数后面的表单的DataContext。类似的东西:

    public partial class MainWindow : Window
    {
        private readonly MainWindowViewModel _vm;

        public MainWindow()
        {
            InitializeComponent();

            var vm = new MainWindowViewModel
            {
                People = new ObservableCollection<Person>(new List<Person>
                {
                    new Person {FirstName = "John", LastName = "Doe"},
                    new Person {FirstName = "Jane", LastName = "Doe"}
                })
            };

            vm.Commit += (sender, args) =>
            {
                MessageBox.Show("You selected: " + _vm.SelectedPerson.FirstName);
            };

            DataContext = vm;
            _vm = vm;
        }
    }

在XAML中,将ItemsSource与视图模型ObservableCollection及其SelectedItem属性绑定到视图模型Selected ...属性,如下所示:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <ComboBox Grid.Row="0" ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Margin="5,0,0,0" Text="{Binding FirstName}"></TextBlock>
                        <TextBlock Margin="5,0,0,0" Text="{Binding LastName}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <TextBlock Grid.Row="1" Text="{Binding SelectedPerson.FirstName}"></TextBlock>
        <Button Grid.Row="2" Command="{Binding CommitSelection}">Click Me</Button>
    </Grid>
</Window>

请参阅TextBlock Grid.Row="1"我绑定的任何内容。 另外,请参阅Button Command ...我将其绑定到ICommand,它本身在被调用时将调用绑定到后面代码的Commit事件处理程序(请参阅{{ 1}})在后面的代码中。

最后,这是示例视图模型代码:

vm.Commit += ...

我很确定您熟悉 public class MainWindowViewModel : ViewModelBase { private Person _selectedPerson; private ObservableCollection<Person> _people; public event EventHandler Commit; public Person SelectedPerson { get { return _selectedPerson; } set { _selectedPerson = value; RaisePropertyChange("SelectedPerson"); } } public ObservableCollection<Person> People { get { return _people; } set { _people = value; RaisePropertyChange("People"); } } public ICommand CommitSelection { get { return new RelayCommand<object>(o => { if (Commit != null) Commit(this, new EventArgs()); }); } } } INotifyPropertyChange模式,因此我会将这些模式排除在外。基本上,viewmodel正在实现RelayCommand,它实现了ViewModelBase辅助函数(INotifyPropertyChange)。我的RaisePropertyChange基本上只是粘合代码,用于将RelayCommand<T>粘贴到EventHandlerICommand模型类基本上是一个带有PersonFirstName字符串属性的POCO。