2016-04-13 8 views
0

コントロールディスプレイのトップエレメントが折りたたまれている場合、どうすればいいですか? リストビューはExpanderにラップされており、折りたたまれたときに最初の5つの要素を表示したいと考えています。シースルーWPFエキスパンダーを参照してください

+0

あなたは、トップの要素を表示するには、それが崩壊したとき、エキスパンダーのヘッダを更新することができますが、これは同じリストビューではありません。それはあなたの要件を満たしていますか? – Dennis

+0

はい、どうすればいいですか? – user1071420

答えて

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)拡大:

enter image description here

2)崩壊:

enter image description here

関連する問題