簡體   English   中英

WP7:有條件地顯示和隱藏數據模板中的控件元素

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM