WPF获取组合框项目模板的选定选项

WPF Getting selected option of combobox item template

我通过数据模板用文本块填充我的组合框,因为这是我能找到的用变量列表填充下拉框的最直接方法。 但是,既然我正在尝试读取值或选定的选项,我不知道如何解决它。 所有其他主题推荐"SelectedValue.ToString();"或类似的,但这只是returns我的XAML..

的第一行

我的Xaml;

<ComboBox Name="DropdownDansen" Grid.Column="1" Grid.Row="2" Margin="5" 
Grid.ColumnSpan="2" SelectedValue="{Binding dans}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding dans}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

我的客服:

    public List<Person> people = new List<Person>();

        public MainWindow()
        {
            InitializeComponent();
            people.Add(new Person { id = "0", dans = "Tango", teamlid1 = "Daniel 
", teamlid2 = "Sabrina ", coach = "Hans van Bommel" });
            people.Add(new Person { id = "1", dans = "Wals", teamlid1 = "de Ridder", teamlid2 = "Aninka ", coach = "Hans van Bommel" });
            people.Add(new Person { id = "2", dans = "Foxtrot", teamlid1 = "de Ridder", teamlid2 = "de Ridder", coach = "Hans van Bommel" });
            people.Add(new Person { id = "3", dans = "Quickstep", teamlid1 = "de Ridder", teamlid2 = "de Ridder", coach = "Dansschool van Amersfoort" });

            DropdownDansen.ItemsSource = people;
            displayDans.DataContext = new DisplayText() { deDans = "chachacha" 
    };
            displaylid1.DataContext = new DisplayText() { lid1 = "Kees" };
            displaylid2.DataContext = new DisplayText() { lid2 = "Hariette" };
            displaycoach.DataContext = new DisplayText() { deCoach = "Steve" };
        }

        public class Person
        {
            public string id { get; set; }
            public string dans { get; set; }
            public string teamlid1 { get; set; }
            public string teamlid2 { get; set; }
            public string coach { get; set; }

        }

编辑: @mm8 提供的答案很管用! 但是,随着组合框的更新,下拉菜单中填满了我的 xaml 的第一行!

 <ComboBox Name="DropdownDansen" Grid.Column="1" Grid.Row="2" Margin="5" Grid.ColumnSpan="2" SelectedValue="{Binding dans}" SelectedValuePath="dans"/>

SelectedItem 转换为 Person:

Person selectedPerson = DropdownDansen.SelectedItem as Person;
if (selectedPerson != null)
{
    string dans = selectedPerson.dans;
}

要使您的绑定 (SelectedValue="{Binding dans}") 正常工作,dans 应该是 ComboBoxDataContextstring 属性您还应该将 SelectedValuePath 属性 设置为 "dans":

<ComboBox Name="DropdownDansen" ... SelectedValue="{Binding dans}" SelectedValuePath="dans">