XAML 带超链接的折叠项目符号列表
XAML Collapsed bullet point list with hyperlink
需要一些关于 XAML 折叠项目符号列表样式的指针,该列表带有超链接以显示所有数据项,即默认情况下仅显示第一个选项,带有一些超链接文本,单击该文本将显示列表中的剩余项目。
例如:
折叠列表
选项 1
点击“点我”展开列表
- 选项 1
- 选项 2
- 选项 3....
不确定这是否正是您要的。通常这不是“请为我编写代码”网站
也就是说,这是一些代码
按照您的要求做并不难。您只需要创建一个自定义用户控件。下面是我为您制作的自定义用户控件的 Xaml 和 Xaml.cs
SampleCollapsedList.xaml.cs
public partial class SampleCollapsedList : UserControl, INotifyPropertyChanged
{
public bool IsExpanded { get; set; }
public string FirstItem
{
get
{
return ListItems != null && ListItems.Count > 0 ? ListItems?.First() : string.Empty;
}
}
public ObservableCollection<string> ListItems
{
get { return (ObservableCollection<string>)GetValue(ListItemsProperty); }
set { SetValue(ListItemsProperty, value); }
}
public static readonly DependencyProperty ListItemsProperty =
DependencyProperty.Register(nameof(ListItems), typeof(ObservableCollection<string>), typeof(SampleCollapsedList), new PropertyMetadata(null,CollectionUpdated));
public event PropertyChangedEventHandler PropertyChanged;
public SampleCollapsedList()
{
InitializeComponent();
}
private static void CollectionUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is SampleCollapsedList sampleCollapsedList)
{
sampleCollapsedList.OnPropertyChanged(nameof(FirstItem));
}
}
private void OnHyperlinkClicked(object sender, RoutedEventArgs e)
{
IsExpanded = !IsExpanded;
OnPropertyChanged(nameof(IsExpanded));
}
private void OnPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
SampleCollapsedList.xaml
<UserControl x:Class="WpfApp1.SampleCollapsedList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800"
x:Name="self">
<UserControl.Resources>
<local:BoolToVisConverter x:Key="BoolToVisibilityConverter" />
</UserControl.Resources>
<Grid Height="250"
Width="500">
<StackPanel Orientation="Vertical"
Visibility="{Binding Path=IsExpanded, ElementName=self, Converter={StaticResource ResourceKey=BoolToVisibilityConverter}, ConverterParameter=opposite}">
<TextBlock Text="{Binding Path=FirstItem, ElementName=self}" />
<TextBlock><Hyperlink Click="OnHyperlinkClicked">Click Me</Hyperlink></TextBlock>
</StackPanel>
<ListBox Visibility="{Binding Path=IsExpanded, ElementName=self, Converter={StaticResource ResourceKey=BoolToVisibilityConverter}}"
ItemsSource="{Binding Path=ListItems, ElementName=self}"
BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock><Run>- </Run><Run Text="{Binding Path=.}" /></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
还有一个简单的转换器,可以实现隐藏显示魔法
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows;
namespace WpfApp1
{
public class BoolToVisConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(parameter is string param && param.ToLower().Contains("opposite"))
{
return (bool)value ? Visibility.Collapsed : Visibility.Visible;
}
else
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
需要一些关于 XAML 折叠项目符号列表样式的指针,该列表带有超链接以显示所有数据项,即默认情况下仅显示第一个选项,带有一些超链接文本,单击该文本将显示列表中的剩余项目。 例如:
折叠列表
选项 1
点击“点我”展开列表
- 选项 1
- 选项 2
- 选项 3....
不确定这是否正是您要的。通常这不是“请为我编写代码”网站
也就是说,这是一些代码
按照您的要求做并不难。您只需要创建一个自定义用户控件。下面是我为您制作的自定义用户控件的 Xaml 和 Xaml.cs
SampleCollapsedList.xaml.cs
public partial class SampleCollapsedList : UserControl, INotifyPropertyChanged
{
public bool IsExpanded { get; set; }
public string FirstItem
{
get
{
return ListItems != null && ListItems.Count > 0 ? ListItems?.First() : string.Empty;
}
}
public ObservableCollection<string> ListItems
{
get { return (ObservableCollection<string>)GetValue(ListItemsProperty); }
set { SetValue(ListItemsProperty, value); }
}
public static readonly DependencyProperty ListItemsProperty =
DependencyProperty.Register(nameof(ListItems), typeof(ObservableCollection<string>), typeof(SampleCollapsedList), new PropertyMetadata(null,CollectionUpdated));
public event PropertyChangedEventHandler PropertyChanged;
public SampleCollapsedList()
{
InitializeComponent();
}
private static void CollectionUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is SampleCollapsedList sampleCollapsedList)
{
sampleCollapsedList.OnPropertyChanged(nameof(FirstItem));
}
}
private void OnHyperlinkClicked(object sender, RoutedEventArgs e)
{
IsExpanded = !IsExpanded;
OnPropertyChanged(nameof(IsExpanded));
}
private void OnPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
SampleCollapsedList.xaml
<UserControl x:Class="WpfApp1.SampleCollapsedList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800"
x:Name="self">
<UserControl.Resources>
<local:BoolToVisConverter x:Key="BoolToVisibilityConverter" />
</UserControl.Resources>
<Grid Height="250"
Width="500">
<StackPanel Orientation="Vertical"
Visibility="{Binding Path=IsExpanded, ElementName=self, Converter={StaticResource ResourceKey=BoolToVisibilityConverter}, ConverterParameter=opposite}">
<TextBlock Text="{Binding Path=FirstItem, ElementName=self}" />
<TextBlock><Hyperlink Click="OnHyperlinkClicked">Click Me</Hyperlink></TextBlock>
</StackPanel>
<ListBox Visibility="{Binding Path=IsExpanded, ElementName=self, Converter={StaticResource ResourceKey=BoolToVisibilityConverter}}"
ItemsSource="{Binding Path=ListItems, ElementName=self}"
BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock><Run>- </Run><Run Text="{Binding Path=.}" /></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
还有一个简单的转换器,可以实现隐藏显示魔法
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows;
namespace WpfApp1
{
public class BoolToVisConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(parameter is string param && param.ToLower().Contains("opposite"))
{
return (bool)value ? Visibility.Collapsed : Visibility.Visible;
}
else
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}