2013-12-21 131 views
0

我正在使用下一個代碼來顯示ListView。 ListView控件包含具有結合到一個變量對我一個的MainPage一個FontSized的TextBlock:列表視圖中的Metro XAML綁定

   <ListView x:Name="ListView" 
       SelectionMode="Single" 
       SelectionChanged="ListView_OnSelectionChanged" 
       Grid.Row="1" 
       Margin="8,16"   
       > 
       <ListView.ItemTemplate> 
         <DataTemplate> 
          <StackPanel> 
           <TextBlock Text="{Binding DisplayName}" 
        FontSize="{Binding Path=FontSizeListViewTitle}" Margin="6,0,0,0" TextWrapping="NoWrap" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListView.ItemTemplate> 
       </ListView> 

的問題是,字號=「{綁定路徑= FontSizeListViewTitle}」似乎並沒有正常工作。它顯示的字體大小接近6(或某些東西),儘管變量FontSizeListViewTitle只取值在16和24之間。

有趣的是,如果我將TextBlock放在ListView之外作品完美。只有當我嘗試在ListView中使用綁定時纔會出現此問題。 此外,文本=「{結合顯示名稱}完美的作品與文本塊表明,它具有顯示文本

從一些的.cs代碼文件現在。

private int _fontSizeListViewTitle; 
    public int FontSizeListViewTitle 
    { 
     get { return _fontSizeListViewTitle; } 
     set 
     { 
      _fontSizeListViewTitle = value; 

      OnPropertyChanged("FontSizeListViewTitle"); 
     } 
    } 

    #region INotifyPropertyChanged implementation 
    public event PropertyChangedEventHandler PropertyChanged; 

    internal void OnPropertyChanged(string propertyName) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    #endregion 

    public MainPage() 
    { 

     InitializeComponent(); 

     this.DataContext = this; 

     ListView.ItemsSource = MyList; // MyList is an Observable Collection 
    } 

回答

0

的DataTemplate中尋找字體大小綁定在列表視圖項目源中而不是整體數據上下文中,您可以在字體大小綁定中使用相對來源,例如:

FontSize =「{Binding Path = DataContext.FontSizeListViewTile,RelativeSource = {RelativeSource TemplatedParent }}「

我現在不在我的電腦前,所以語法可能會略微偏離,但您明白了。 MS文檔:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.binding.relativesource.aspx

的文檔沒有提到祖先的特徵,所以我不知道這是在WinRT的庫可用。

+0

它不會返回任何錯誤,但是當我運行程序並嘗試將元素添加到ListView時,應用程序崩潰。 – TheQuestioner

+0

如果我嘗試做一些像並從代碼更改FontSize的東西會怎麼樣?問題是我不知道如何訪問TitleTextBlock。 ListView.TitleTextBlock不起作用。 – TheQuestioner

3

ListViewItem的DataContext是Observable集合中的對象。當您在ListView的ItemTemplate中聲明TextBlock的文本綁定爲{Binding DisplayName}而不是{Binding MyList.DisplayName}{Binding MyList[0].DisplayName}或其他任何東西時,您可以看到它。並且FontSize="{Binding FontSizeListViewTitle}"將不起作用,因爲Observable中的對象不具有FontSizeListViewTitle屬性。

解決的辦法是你把FontSizeListViewTitle性質類似以下內容字號綁定到ListView控件的DataContext的:

<ListView> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding DisplayName}" Margin="6,0,0,0" TextWrapping="NoWrap" 
          FontSize="{Binding Path=DataContext.FontSizeListViewTile, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

UPDATE:

由於FindAncestor在WinRT中失蹤,最簡單的解決辦法是to use ElementName到找到ListView,然後將FontSize綁定到ListView的DataContext。

<ListView x:Name="listView"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding DisplayName}" Margin="6,0,0,0" TextWrapping="NoWrap" 
          FontSize="{Binding ElementName=listView, Path=DataContext.FontSizeListViewTile}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 
+0

AncestorType和x:type顯然不被識別。 – TheQuestioner

+0

是的,我沒有注意到問題是WinRT我的代碼在WPF中工作。我將更新與WinRT的工作.. – har07

+0

是的,仍然無法正常工作。它不識別FontSizeListViewTile,因爲「DataContext的類型是'object'」。 – TheQuestioner