如果Expander
控件在折叠时显示顶部元素,该怎么办?
我有一个包含在Expander
内的列表视图,我希望它在崩溃时显示5个第一个元素。
答案 0 :(得分:2)
您可以使用样式触发器更改标题 像这样:
public class ViewModel
{
private const string sampleText =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eget risus sit amet dolor malesuada scelerisque.";
public ViewModel()
{
Items = new List<string>(sampleText.Split());
Header = "Items:";
CollapsedHeader = $"Items: {string.Join(" ", Items.Take(5))}...";
}
public IEnumerable<string> Items { get; }
public string Header { get; }
public string CollapsedHeader { get; }
}
XAML:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Expander>
<Expander.Style>
<Style TargetType="{x:Type Expander}">
<Style.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter Property="Header" Value="{Binding Header}"/>
</Trigger>
<Trigger Property="IsExpanded" Value="False">
<Setter Property="Header" Value="{Binding CollapsedHeader}"/>
</Trigger>
</Style.Triggers>
</Style>
</Expander.Style>
<ListBox ItemsSource="{Binding Items}" />
</Expander>
</Window>
结果:
1)扩大:
2)崩溃了: