访问 ListView CellTemplate UIElements

Access ListView CellTemplate UIElements

我正在制作一个简单的列表,其中只有很少的选项。 这是它的样子:

我的问题是:

还有什么我想做但做不到的是:

我希望有人能帮助我。 到目前为止,我的代码如下所示:

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));
    }
}

项目:

MusicList.sln

当然 - 抱歉我的英语不好 :)

您可以创建一个值转换器,它将采用您的按钮文本和 return 基于文本值的可见性值。像这样;

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(); 
    }
}

通过创建静态资源并将其添加到您的资源字典中,例如在您的 xaml 中创建此 class 的实例像这样;

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

然后将滑块可见性绑定到按钮文本;

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