需要一些關于 XAML 樣式的指標,用于帶有超鏈接的折疊專案符號串列以顯示所有資料項,即默認情況下僅顯示第一個選項,并帶有一些超鏈接文本,當單擊時,將顯示串列中的剩余專案。例如:
折疊串列
選項1

也就是說,這里有一些代碼
做你所要求的并不難。您只需要創建一個自定義用戶控制元件。下面是我為您制作的自定義用戶控制元件的 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(); } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424221.html標籤:C# 。网 wpf xml .net-framework-4.8
