LetzerWille
LetzerWille

Reputation: 5668

DisplayMemberPath on certain click only

in Search_Click the source of lstBox is List<string> in btnStats_Click the source of lstBox il List<Genre>

after I have added DisplayMemberPath="Name" as listBox property, the Search_Click does not output anything.

Is the a way to apply DisplayMemberPath="Name" to lstBox, only on btnStats_Click ?

public class Genre
{
    public string Name { get; set; }
    public int Count { get; set; }
    public double Size { get; set; }
    public string Drive { get; set; }
}

private void Search_Click(object sender, RoutedEventArgs e)
{
    var path = Constants.allMoviesPath;

    var ext = new List<string> { @".txt", @".ini", @".exe", @".mob", @".srt", @".ass" };

    lstBox.ItemsSource = Directory.GetFiles(path, "*" + SearchString + "*", SearchOption.AllDirectories)
                .Where(f => !ext.Contains(System.IO.Path.GetExtension(f)))
               .Select(f => System.IO.Path.GetFileNameWithoutExtension(f))
               .ToList();
}

private void btnStats_Click(object sender, RoutedEventArgs e)
{
    lstBox.ItemsSource = FileLists.MoviesCountSizeStats();
}

return type of MoviesCountSizeStats() is List<Genre>

<ListBox
    x:Name="lstBox"
    Background="CadetBlue"
    FontFamily="Consolas"
    DisplayMemberPath="Name"
    FontSize="14"
    FontWeight="DemiBold"
    ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
    MouseDoubleClick="lstBox_MouseDoubleClick" />

Upvotes: 0

Views: 28

Answers (1)

This is easy. If you want to change lstBox.DisplayMemberPath... just change it.

private void Search_Click(object sender, RoutedEventArgs e)
{
    var path = Constants.allMoviesPath;

    var ext = new List<string> { @".txt", @".ini", @".exe", @".mob", @".srt", @".ass" };

    lstBox.ItemsSource = Directory.GetFiles(path, "*" + SearchString + "*", SearchOption.AllDirectories)
                .Where(f => !ext.Contains(System.IO.Path.GetExtension(f)))
               .Select(f => System.IO.Path.GetFileNameWithoutExtension(f))
               .ToList();

    //  Null when it should be null
    lstBox.DisplayMemberPath = null;
}

private void btnStats_Click(object sender, RoutedEventArgs e)
{
    lstBox.ItemsSource = FileLists.MoviesCountSizeStats();


    //  "Name" when it should be "Name"
    lstBox.DisplayMemberPath = "Name";
}

Upvotes: 1

Related Questions