访问ListView CellTemplate UIElements

时间:2016-05-07 13:13:36

标签: c# wpf listview gridview celltemplate

我正在制作一个包含少量选项的简单列表。 这是它的样子: enter image description here

  • 按下listviewitem时出现按钮,离开
  • 时消失
  • 按播放时,此按钮保持可见,内容更改为停止

我的问题是:

  • 当我按停止时,此按钮保持可见,触发器消失:/

我还想做什么,但我不能做到:

  • 当我按下Play然后出现Slider,否则折叠

我希望有人可以帮助我。 到目前为止,我的代码看起来像这样:

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

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

1 个答案:

答案 0 :(得分:1)

您可以创建一个值转换器,它将获取按钮的文本并根据文本值返回可见性值。像这样的东西;

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中创建此类的实例,例如像这样的东西;

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

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

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