简体   繁体   中英

Combobox with data Template value is not selected

I have a combobox with an DataTemplate when i select an element just the Name of the Selected element is shown as selected. What can i do that the name of the Selected elment is shown in Selection as in the opened Combobox shown.

XAML of the Combobox:

<ComboBox  
            ItemsSource="{Binding}"

            Margin="51,146,238,146"
            BorderThickness="0"
            HorizontalAlignment="Stretch" 
            HorizontalContentAlignment="Stretch"                                                   
            Padding="3" Height="20" IsEditable="True"

            SelectedValuePath="Name"

            x:Name="testCombobox" SelectionChanged="testCombobox_SelectionChanged">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBox
                        Text="{Binding Path=Name}"

                        Padding="1,1,1,1"
                        AcceptsReturn="True"
                        Background="Yellow">
                    </TextBox>
                </DataTemplate>
            </ComboBox.ItemTemplate>

Code in the main window:

public partial class MainWindow : Window
    {



        ObservableCollection<Person> personen = new ObservableCollection<Person>();

        public MainWindow()
        {

            InitializeComponent();
            //testCombobox.ItemsSource = LoadComboBoxData;

            personen.Add(new Person("Meier dsg dsfg sdgf sdfg sdfg", 67));
            personen.Add(new Person("Fischer sdfgs sdfg sg sgssg sf", 23));
            personen.Add(new Person("Schmidt sdfg sdfg sgf sfdg sf sdfqwe  qwer  qwe", 45));
            personen.Add(new Person("Serna asdfd sdf dsf sdf sdf dsf dfa  qwer qwe", 26));
            personen.Add(new Person("Müller asdf a aafd aff dfda adf asdf qwer qwer ", 39));
            this.DataContext = personen;

        }

Class Person:

public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }




        private string _Name;
        private int _Alter;
        public Person(string name, int alter)
        {
            Name = name;
            Alter = alter;
        }

        public int Alter
        {
            get { return _Alter; }
            set
            {
                _Alter = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Alter"));
            }
        }
        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }

    }

there are two problem you might get; First ItemsSource="{Binding}" cann't get the datasoure as you want. and the other mistake is the bingding path should be a public property so ObservableCollection personen = new ObservableCollection(); should be change as property like this:

public ObservableCollection<Person> Person
{
get
{
 return person;
}
set
{
  person=value;
  OnPropertyChange("Personen")
}
}
private  ObservableCollection<Person> personen = new ObservableCollection<Person>()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM