Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 获取在可编辑组合框中键入的选定值?_Wpf_Combobox - Fatal编程技术网

Wpf 获取在可编辑组合框中键入的选定值?

Wpf 获取在可编辑组合框中键入的选定值?,wpf,combobox,Wpf,Combobox,我有一个组合框,它是可编辑的。因此,用户可以选择一个项目,但如果该项目不存在,他可以随时键入他想要的内容。但我的问题是,如果我选择了一个现有项目,那么一切都正常,并且设置了值: <ComboBox Height="23" SelectedIndex="0" HorizontalAlignment="Left" Margin="104,73,0,0" Name="comboBox1" VerticalAlignment="Top" Width="159" IsEditable="T

我有一个组合框,它是可编辑的。因此,用户可以选择一个项目,但如果该项目不存在,他可以随时键入他想要的内容。但我的问题是,如果我选择了一个现有项目,那么一切都正常,并且设置了值:

  <ComboBox  Height="23"  SelectedIndex="0"  HorizontalAlignment="Left" Margin="104,73,0,0" Name="comboBox1" VerticalAlignment="Top" Width="159" IsEditable="True" SelectionChanged="comboBox1_SelectionChanged" />

 private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ToetsAlgemeneGegevensViewModel vm = (ToetsAlgemeneGegevensViewModel)this.DataContext;
            if (comboBox1.SelectedValue != null && vm != null)
            {


                vm.Examination.Course = comboBox1.SelectedValue.ToString();
            }

private void comboBox1\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
toetsalgemengevensviewmodel vm=(toetsalgemengevensviewmodel)this.DataContext;
if(comboBox1.SelectedValue!=null&&vm!=null)
{
vm.examice.Course=comboBox1.SelectedValue.ToString();
}
但是,如果我输入了什么,我如何设置这个值?知道如何设置的人?

快速回答:

我认为您最好使用
ComboBox.Text
属性。在视图模型中创建一个字符串属性,并将其绑定到Text属性中:
Text=“{Binding MyStringProperty}”

在字符串属性的setter中执行在
组合框1\u SelectionChanged
中执行的操作。我认为这就足够了