gpt4 book ai didi

c# - 访问 ListView CellTemplate UIElements

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:35 24 4
gpt4 key购买 nike

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

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

我的问题是:

  • 当我按下 Stop 时,此按钮保持可见并且触发器消失:/

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

  • 当我按下 Play 时,Slider 出现,否则 Collapsed

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

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

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

最佳答案

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

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 可见性绑定(bind)到按钮文本;

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

关于c# - 访问 ListView CellTemplate UIElements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com