[英]WPF Getting selected option of combobox item template
I populate my Combobox with textblocks via a datatemplate, since this was the most direct way i could find to populate the dropdown box with a list of variables. 我通过数据模板用文本块填充组合框,因为这是我可以找到的使用变量列表填充下拉框的最直接方法。 However, now that I am trying to read the value or selected option, I have no idea how to tackle it.
但是,由于我现在正在尝试读取值或选定的选项,所以我不知道如何解决它。 All other topics recommend "SelectedValue.ToString();"
所有其他主题建议“ SelectedValue.ToString();” or the likes, but this just returns the first line of my XAML..
等等,但这只是返回我的XAML的第一行。
My 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>
my cs: 我的CS:
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; }
}
Edit: The answer provided by @mm8 does quite the trick! 编辑:@ mm8提供的答案确实很成功! However, with the combobox updated, the dropdown menu is filled with the first line of my xaml instead!
但是,在组合框更新后,下拉菜单中填充了我的xaml的第一行!
<ComboBox Name="DropdownDansen" Grid.Column="1" Grid.Row="2" Margin="5" Grid.ColumnSpan="2" SelectedValue="{Binding dans}" SelectedValuePath="dans"/>
Cast SelectedItem
to a Person
: 将
SelectedItem
投射到一个Person
:
Person selectedPerson = DropdownDansen.SelectedItem as Person;
if (selectedPerson != null)
{
string dans = selectedPerson.dans;
}
For your binding ( SelectedValue="{Binding dans}"
) to work, dans
should be a string
property of the DataContext
of the ComboBox
and you should also set the SelectedValuePath
property to "dans": 为了使绑定(
SelectedValue="{Binding dans}"
)起作用, dans
应该是ComboBox
的DataContext
的string
属性,并且还应该将SelectedValuePath
属性设置为“ dans”:
<ComboBox Name="DropdownDansen" ... SelectedValue="{Binding dans}" SelectedValuePath="dans">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.