WPF/C# Combobox Only Changes Selection When A Click is Detected Outside of Text Area
So I have several comboboxes in my WPF application that don't change the selection when a user clicks on the text of a combobox item. In order to select a particular item you have to click to the right or left of the text. I have another combobox that selects just fine when the text is clicked. The only difference between the two is databinding. The comboboxes that don't select when the text is clicked are databound to an ObservableCollection of one type or another. The combobox that works has manually inserted, static values.
I've searched the issue extensively and I can't seem to find anyone else who has had this issue or anything remotely similar. I'm not setting any weird properties.
Here is the code for one of the problematic comboboxes:
<ComboBox HorizontalAlignment="Left" Margin="40,160,0,0" VerticalAlignment="Top" Width="132" ItemsSource="{Binding Path=Systems}" SelectedItem="{Binding Path=System}" SelectedIndex="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<ComboBoxI开发者_StackOverflow社区tem Content="{Binding Path=Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Here is a video of the behavior:
http://www.youtube.com/watch?v=D0r1N1ghw-k
enter code here
Suppose my Combobox is defind as below
<ComboBox Name="cmb" Width="200" Height="20" DisplayMemberPath="PersonName" SelectedValuePath="PersonID">
</ComboBox>
please notice , i have removed the itemtemplate part
and my model is as below
public class Person
{
public string PersonName { get; set; }
public string PersonID { get; set; }
}
And my binding is in code behind , this step is not necessary , you can do it in the xaml , iam just checking quickly so much dirty code
public List<Person> source = new List<Person>();
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 20; i++)
{
source.Add(new Person() { PersonID = i.ToString(), PersonName = "Sau" + i.ToString() });
}
cmb.ItemsSource = source;
this.DataContext = this;
}
so if you run this sample , you will see you can select the value when you click on the text itself.
it's content property which causes some problem but i am not 100% sure on this.
精彩评论