2012-01-19 15 views
1

のは、我々は、単純なデータクラスを持っているとしましょう:変更ListView.ItemTemplateサブ要素の変化に

public class Ex { 
    public string Prop1 {...} // notify property 
    public string Prop2 {...} // notify property 
} 

と、このクラスのオブジェクトののObservableCollection。私は、このコレクションを別々のDataTemplatedでListViewに表示したいのですが、これはEx.Prop2で区別されます(nullまたは空の場合はtemplate01、それ以外の場合はtemplate02)。このプロパティは、実行時に変更することができますので、ListView.ItemTemplateSelectorを使用した簡単な "トリック"は機能しません:(

この機能を実現する方法は、コレクションの各オブジェクトでNotifyPropertyChangedをリッスンする以外の方法そして、テンプレートを手動で変更するよりも、あなたの助けのための

おかげ

私が既に持っているコードの部分の下

:?。

<ListView x:Name="lstTerms" 
    ItemsSource="{Binding Game.Words}" 
    HorizontalContentAlignment="Stretch" 
    Grid.IsSharedSizeScope="True"> 
    <ListView.ItemContainerStyle> 
     <Style> 
      <Setter Property="Control.Padding" Value="0" /> 
     </Style> 
    </ListView.ItemContainerStyle> 

    <!-- checks if element is null or its Prop2 is null or empty. If so, uses NullTemplate --> 
    <ListView.ItemTemplateSelector> 
     <local:MySelectTemplate 
      NormalTemplate="{StaticResource NormalItemTemplate}" 
      NullTemplate="{StaticResource NullItemTemplate}" /> 
    </ListView.ItemTemplateSelector> 
</ListView> 

答えて

2

代わりのTemplateSelectorを使用して、あなたは、Siを持つことができますプロパティ値に応じて可視性を切り替える2つのグリッドを含むDataTemplateを設定します。ここで

は一例です:

明らか
<ListView.ItemTemplate> 
    <DataTemplate> 
     <Grid> 
      <Grid Background="LightBlue" Name="normalGrid"> 
       <Grid.Style> 
        <Style> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding Path=Prop1}" Value="{x:Null}"> 
           <Setter Property="Grid.Visibility" Value="Hidden"></Setter> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Grid.Style> 
       <TextBlock Text="{Binding Prop1}"></TextBlock> 
      </Grid> 
      <Grid Background="Green" Name="nullGrid"> 
       <Grid.Style> 
        <Style> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=normalGrid, Path=Visibility}" Value="Visible"> 
           <Setter Property="Grid.Visibility" Value="Hidden"></Setter> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Grid.Style> 
       <TextBlock Text="{Binding Prop2}"></TextBlock> 
      </Grid> 
     </Grid> 
    </DataTemplate> 
</ListView.ItemTemplate> 

あなたは2 DataTemplatesを表すユーザーコントロールとのTextBlock要素を置き換えることができます。

必要に応じて、Grid.VisibilityをViewModelのプロパティ(IsVisibleなど)にバインドして、VisibilityConverterを使用して、かさばるStylesの必要性を取り除くこともできます。

+0

を望んでいたが、レイチェルのも良いです場合String.IsNullOrEmpty(value)を返しConverter使用することができ、変更取得値に対応しています。なぜ私は自分でそれらのことを考えなかったのかわかりません...多くの仕事であなたを殺します; D – SOReader

1

私は通常、DataTriggerに基づいてContentTemplateを変更するContentControlを使用します。 DataTemplateSelectorsはない

<Style x:Key="SomeStyleKey" TargetType="{x:Type ContentControl}"> 
    <Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}" /> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding Prop2}" Value="{x:Null}"> 
      <Setter Property="ContentTemplate" Value="{StaticResource NullTemplate}" /> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding Prop2}" Value=""> 
      <Setter Property="ContentTemplate" Value="{StaticResource NullTemplate}" /> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

... 

<ListView.ItemTemplate> 
    <DataTemplate> 
     <ContentControl Style="{StaticResource SomeStyleKey}" /> 
    </DataTemplate> 
</ListView.ItemTemplate> 

を行いながらDataTriggersはまた、あなたは、私が実際にこのアプローチを使用し、単一のDataTrigger