【问题标题】:Metro XAML Binding inside ListViewListView 内的 Metro XAML 绑定
【发布时间】:2014-01-10 10:30:26
【问题描述】:

我正在使用下一个代码来显示 ListView。 ListView 包含一个 TextBlock,它的 FontSized 绑定到我的 MainPage 上的一个变量:

              <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>

问题在于 FontSize="{Binding Path=FontSizeListViewTitle}" 似乎无法正常工作。尽管变量 FontSizeListViewTitle 仅采用 16 到 24 之间的值,但它向我显示了 fontSize 接近 6(或其他值)的文本。

有趣的是,如果我将 TextBlock 放在 ListView 之外,它可以完美运行。仅当我尝试在 ListView 中使用绑定时才会出现问题。 此外,Text="{Binding DisplayName} 完美运行,TextBlock 显示它必须显示的文本。

现在来自 .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
    }

【问题讨论】:

    标签: c# xaml listview data-binding microsoft-metro


    【解决方案1】:

    数据模板正在列表视图项目源中查找字体大小绑定,而不是整个数据上下文。您可以在字体大小绑定中使用相对源,如下所示:

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

    我现在不在我的电脑前,所以语法可能略有偏差,但你明白了。微软文档:

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

    该文档未提及祖先功能,因此我不确定 WinRT 库中是否提供该功能。

    【讨论】:

    • 它不会返回任何错误,但是当我运行程序并尝试将元素添加到 ListView 时,应用程序崩溃了。
    • 如果我尝试执行类似 并从代码更改 FontSize 怎么办?问题是我不知道如何访问 TitleTextBlock。 ListView.TitleTextBlock 不工作。
    【解决方案2】:

    ListViewItem 的 DataContext 是 Observable Collection 中的对象。当在 ListView 的 ItemTemplate 中将 TextBlock 的 Text 绑定声明为 {Binding DisplayName} 而不是 {Binding MyList.DisplayName}{Binding MyList[0].DisplayName} 或其他任何内容时,您可以看到它。而FontSize="{Binding FontSizeListViewTitle}" 将不起作用,因为 Observable 中的对象没有 FontSizeListViewTitle 属性。

    解决方案是将 FontSize 绑定到 ListView 的 DataContext 中,在其中放置 FontSizeListViewTitle 属性,如下所示:

    <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>
    

    更新:

    由于 WinRT 中缺少 FindAncestor,最简单的解决方法是 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>
    

    【讨论】:

    • AncestorType 和 x:type 显然无法识别。
    • 是的,我没有注意到问题是 WinRT 我的代码在 WPF 中工作。我将更新 WinRT 的解决方法..
    • 是的,还是不行。它无法识别 FontSizeListViewTile,因为 "DataContext is of type 'object'" 。
    猜你喜欢
    • 1970-01-01
    • 2014-10-31
    • 2011-08-16
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 2014-12-08
    • 1970-01-01
    相关资源
    最近更新 更多