2013-04-25 189 views
0

您好我想綁定一個DataTemplate內的textBlock的值,TextBlock的text屬性將根據文件/文件夾列表更改運行時。我寫了下面的代碼,但字符串爲空。 我的工作ENV是的Windows Phone 8與Visual Studio 2012如何綁定DataTemplate中的TextBlock的值?

<Grid x:Name="ContentPanel"> 
<phone:LongListSelector>  
    <phone:LongListSelector.ListFooterTemplate > 
     <DataTemplate > 
      <TextBlock Name="tbfooter" Text="{Binding FooterText, Mode=OneWay}" /> 
     </DataTemplate> 
    </phone:LongListSelector.ListFooterTemplate> 
</phone:LongListSelector> 

這個文本塊名= tbfooter必須與Footertext值更新運行。

現在在我的代碼隱藏我已經定義了這個屬性好像

private int _footerText; 
public int FooterText 
{ 
    get 
    { 
     return this._footerText; 
    } 
    set 
    { 
     this._footerText=value 
     NotifyPropertyChanged("FooterText"); 
    } 
} 

但是德文本塊tbfooter的值爲null,它沒有顯示任何東西它只是空。有人可以幫我嗎?

編輯:我在這裏再次更新了XAML代碼。我在這裏不遵循MVVM,它是簡單的Windows Phone應用程序。任何幫助表示讚賞。

+2

也許你錯過了this._footerText =屬性設置器內的值。 – jure 2013-04-25 09:03:04

+0

感謝您的糾正,但我仍然無法獲得價值。在我看來,TextBlock名稱= tbfooter,不能從CodeBehind訪問,原因何在? – Debhere 2013-04-25 09:13:57

回答

0
private int _footerText; 
public int FooterText 
{ 
    get 
    { 
     return this._footerText; 
    } 
    set 
    { 
     this._footerText=value; // <<-----------You might miss this! 
     NotifyPropertyChanged("FooterText"); 
    } 
} 
0

它看起來像在你的屬性設置你需要通知有關其改變之前達到設定值時,請嘗試這樣的事情

private int _footerText; 
public int FooterText 
{ 
    get 
    { 
     return this._footerText; 
    } 
    set 
    { 
     this._footerText = value; 
     NotifyPropertyChanged("FooterText"); 
    } 
} 
+0

感謝您的糾正,但我仍然無法獲得價值。在我看來,TextBlock的tbfooter名稱不能從CodeBehind訪問,原因何在? – Debhere 2013-04-25 09:13:18

0

當使用DataTemplateDataTemplateDataContext在當前選定的項目。如果您將LongListSelector綁定到T類型的列表,則可以通過此類型T的綁定來訪問適當的屬性。

您想綁定到Viewmodel的非當前DataContext屬性。爲此,您的結果爲空。

試試這個代碼

<Grid x:Name="ContentPanel"> 
<phone:LongListSelector>  
    <phone:LongListSelector.ListFooterTemplate > 
     <DataTemplate > 
      <TextBlock Name="tbfooter" 
        DataContext="{Binding Path=DataContext,RelativeSource={RelativeSource AncestorType=UserControl}}" 
        Text="{Binding FooterText, Mode=OneWay}" /> 
     </DataTemplate> 
    </phone:LongListSelector.ListFooterTemplate> 
</phone:LongListSelector> 
0

正如INXS提到的,你TextBlock是空的,因爲它不綁定到合適的物業。看看this answer,它說明了如何綁定到DataContext上的兩個屬性以及代碼隱藏中的屬性。

相關問題