[英]WP7: Conditionally show & hide control elements in Data Templates
我有一個帶有ListBox控件的便箋應用程序,該控件列出了ObservableCollection<Note> Notes
所有可用ObservableCollection<Note> Notes
。 class Note
具有類似的屬性
String Title;
bool Has_Reminder;
DateTime Reminder_Date;
我想要的是,如果Has_Reminder
為true,則僅顯示TextBlock元素,該元素顯示Reminder_Date
。 但是我不知道如何從我的自定義控件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>
你知道如何使用轉換器嗎? 您的轉換器會將bool轉換為Visibility,然后可以將TextBlock的Visibility綁定到Has_Reminder
:
<TextBlock x:Name="Reminder_Date" Text="{Binding Reminder_Date}" Visibility="{Binding Has_Reminder, Converter={...}}"/>
這可能會有所幫助: http : //www.jeff.wilcox.name/2008/07/visibility-type-converter/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.