Search code examples
c#wpflistviewgridviewcelltemplate

Access ListView CellTemplate UIElements


i am making a simple list with few options. This is how it looks like: enter image description here

  • Buttons appear when hit the listviewitem and disappear when leave
  • When press Play, then this button stays Visible and Content changes to Stop

My problems is:

  • When i press Stop, then this Button stays Visible and Triggers disappear :/

What else i want to do, but i can't is:

  • When i press Play then Slider appears, otherwise Collapsed

I hope that someone can help me. My code looks like this so far:

XAML:

    <ListView Name="lst">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Button Name="btnDownload" Content="Download" Visibility="Hidden" MinWidth="100"/>
                            <Button Name="btnPlay"
                                    Click="btnPlay_Click"
                                    Content="Play"
                                    Visibility="Hidden"
                                    MinWidth="100"/>
                        </StackPanel>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding
                                         RelativeSource={RelativeSource
                                         Mode=FindAncestor,
                                         AncestorType={x:Type ListViewItem}},
                                         Path=IsMouseOver}"
                                         Value="True">
                                <Setter TargetName="btnDownload"
                                        Property="Visibility"
                                        Value="Visible"/>
                                <Setter TargetName="btnPlay"
                                        Property="Visibility"
                                        Value="Visible"/>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn>
                <GridViewColumn.Header>
                    <GridViewColumnHeader Tag="Name">Name</GridViewColumnHeader>
                </GridViewColumn.Header>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel MinWidth="200">
                            <TextBlock Text="{Binding Name}"/>
                            <Slider Name="Slider" Visibility="Visible"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

CS:

public partial class MainWindow : Window
{
    public ListCollectionView MyCollectionView { get; set; }
    public ObservableCollection<Songs> songs = new ObservableCollection<Songs>();
    public MainWindow()
    {
        InitializeComponent();
        MyCollectionView = new ListCollectionView(songs);
        lst.ItemsSource = MyCollectionView;

        songs.Add(new Songs(){Name = "Eminem - Superman"});
        songs.Add(new Songs(){Name = "Rihanna - Please don't stop the music"});
        songs.Add(new Songs(){Name = "Linkin Park - Numb"});
    }

    private void btnPlay_Click(object sender, RoutedEventArgs e)
    {
        //Reset all songs
        List<Button> buttons = FindVisualChildren<Button>(lst).ToList();
        foreach (Button button in buttons)
        {
            button.Content = "Play";
            //Loosing Triggers
        }

        //Play current
        Button btn = sender as Button;
        btn.Visibility = Visibility.Visible;
        btn.Content = "Stop";
    }

    private IEnumerable<T> FindVisualChildren<T>(DependencyObject obj) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }
            else
            {
                var childOfChild = FindVisualChildren<T>(child);
                if (childOfChild != null)
                {
                    foreach (var subchild in childOfChild)
                    {
                        yield return subchild;
                    }
                }
            }
        }
    }
}
public class Songs : INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

Project:

MusicList.sln

And ofcourse - sorry for my bad english :)


Solution

  • You could create a value converter that would take the text of your button and return a visibility value based on the text value. Something like this;

    public class StringToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var buttonText = (string) value;
            switch (buttonText.ToLower())
            {
                case "stop":
                    return Visibility.Visible;
                default:
                    return Visibility.Collapsed;
            }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException(); 
        }
    }
    

    Create an instance of this class in your xaml by creating a static resource and adding it into your resource dictionary e.g. something like this;

    <Window.Resources>
        <myNamespace:StringToVisibilityConverter x:Key="StringToVisibilityConverter"/>
    </Window.Resources>
    

    Then bind your slider visibility to your button text;

    <Slider Name="Slider" Visibility="{Binding ElementName=btnPlay, Path=Content, Converter={StaticResource StringToVisibilityConverter}}"/>