Answer the question
In order to leave comments, you need to log in
How to bind a collection to a ComboBox?
Hello, there is an "Owner" class in a WPF application
public class Owner : INotifyPropertyChanged
{
private string _name; // имя владельца
public event PropertyChangedEventHandler PropertyChanged;
public Owner(string name)
{
_name = name;
}
public Owner()
{
_name = "Анон";
}
public string Name
{
get => _name;
set
{
_name = value;
OnPropertyChanged();
}
} // Name
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
public override string ToString()
{
return $"{_name}";
}
}
<ListBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding Owners}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<TextBlock FontSize="18" Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ComboBox Margin="5 5 5 0"
ItemsSource="{Binding Owners}"
DisplayMemberPath="Name"
Text = "{Binding Owner, UpdateSourceTrigger = PropertyChanged}" />
Answer the question
In order to leave comments, you need to log in
<ComboBox Margin="5 5 5 0"
ItemsSource="{Binding Owners}"
DisplayMemberPath="Name"
Text = "{Binding Owner, UpdateSourceTrigger = PropertyChanged}" />
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question