简体   繁体   中英

Populate TextBox-es depending on selected value in ListBox XAML

So, I have this class

public class Multimedia : INotifyPropertyChanged
{
    private string title;
    private string artist;
    private string genre;
    private MediaType type;
public Multimedia (string _title, string _artist, string _genre, MediaType _type)
{
    this.title = _title;
    this.artist = _artist;
    this.genre = _genre;
    this.type = _type;
}

public String Title { get { return this.title; } }
public String Artist { get { return this.artist; } }
public String Genre { get { return this.genre; } }
public MediaType Type { get { return this.type; } }

public event PropertyChangedEventHandler PropertyChanged;

}

A collection in which I store the objects:

public class MultiMediaList : ObservableCollection<Multimedia>
    {
        public MultiMediaList()
        {
            Add(new Multimedia("YMCA", "Village People", "disco", MediaType.CASSETTE));
            Add(new Multimedia("Free", "Rudimental", "D&B/Liquid", MediaType.DVD));
            Add(new Multimedia("November Rain", "Guns'n'Roses", "rock", MediaType.CD));
        }
    }

And the XAML of the ListBox:

<ListBox Name="LB_media" SelectionChanged="ListBox_SelectionChanged" DisplayMemberPath="Title" />

Code-behind of the Window:

MultiMediaList mediaList;
        public MainWindow()
        {
            InitializeComponent();
            mediaList = new MultiMediaList();
            LB_media.ItemsSource = mediaList;
        }

The ListBox I populate with the Title property value of the objects in the collection. Now, I have code-behind function that populates 3 other TextBox elements (for the properties Title , Artist and Genre and I want to populate them with the corresponding property values, depending on which item in the ListBox is selected. But, can I do that in XAML?

You have to set the DataContext for the surrounding container:

<StackPanel DataContext="{Binding ElementName=LB_media, Path=SelectedItem}">
    <TextBox x:Name="tbArtist" Text="{Binding Artist}" />
    [..]
</StackPanel>

Of couse you could access the SelectedItem directly but this solution is more elegant and robust to changes. If you decide to alter the DataContext you'd only have to replace one line.

You can bind to property of SelectedItem in your ListBox

<StackPanel>
    <TextBox Text="{Binding ElementName=LB_media, Path=SelectedItem.Artist, Mode=OneWay}" IsReadOnly="True"/>
    <TextBox Text="{Binding ElementName=LB_media, Path=SelectedItem.Genre, Mode=OneWay}" IsReadOnly="True"/>
    <TextBox Text="{Binding ElementName=LB_media, Path=SelectedItem.Type, Mode=OneWay}" IsReadOnly="True"/>
</StackPanel>

because you use TextBox and property is read-only you must use Mode=OneWay otherwise you'll have some exceptions

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