2012-05-05 7 views
0

私はObservableCollection<Note> Notesから利用可能なすべてのメモをリストボックスコントロールでノートアプリケーションを持っています。 class Noteは、私が欲しいものWP7:条件付きでデータテンプレート内のコントロール要素を表示および非表示

String Title; 
bool Has_Reminder; 
DateTime Reminder_Date; 

などの属性を持っていることはHas_Reminderがtrueの場合Reminder_Dateを示したのTextBlock要素は、のみ、表示されていることです。しかし、私はカスタムコントロールのNoteListItemからこの属性にアクセスする方法を知らない。そのthis.DataContext属性はnullですが、コントロールはListBox ItemsSourceによって渡されたNoteのバインドされた属性を適切に表示します。それをどうすれば実現できますか?

ありがとうございました。

public NoteListItem() 
{ 
    InitializeComponent(); 

    Note this_note = LayoutRoot.DataContext as Note; // turns out, this_note is null 

    if (!this_note.Has_Reminder) 
     Reminder_Info.Visibility = System.Windows.Visibility.Collapsed; 
} 

NoteListItem制御

<Grid x:Name="LayoutRoot" > 
    <TextBlock x:Name="Title" Text="{Binding Title}" /> 
    <TextBlock x:Name="Reminder_Date" Text="{Binding Reminder_Date}" /> 
</Grid> 

NoteList制御:

<ListBox x:Name="NoteListBox" ItemsSource="{Binding Notes}" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <local:NoteListItem /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

私は動作しませんでしたコンストラクタ内の属性を、読み込もうとしましたが3210

答えて

3

コンバータの使い方は知っていますか?あなたのコンバータは、あなたがHas_ReminderへのTextBlockの可視性をバインドすることができ、視認性にブール値に変換します:

<TextBlock x:Name="Reminder_Date" Text="{Binding Reminder_Date}" Visibility="{Binding Has_Reminder, Converter={...}}"/> 

これは役立つかもしれない:http://www.jeff.wilcox.name/2008/07/visibility-type-converter/

+0

最後に、あなたのソースをチェックアウトする時間を持っていました。うわー、それは簡単でした!この前に私はどのようにコンバータを使用するか分かりませんでした。そのガイドをありがとう! – Joern

関連する問題