【发布时间】:2019-05-27 00:27:40
【问题描述】:
我试图将 DataTemplate 从 ListView 移动到资源字典中,但它以某种方式破坏了绑定,我相信。
我验证了当我对它在列表视图中显示的 Textblock 文本进行硬编码时,似乎列表视图数据源绑定正在工作,它只是无法显示我的数据。
这是字典:
<ResourceDictionary
x:Class="Marathon.Resources.ListViewTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Marathon">
<DataTemplate x:Key="LVTemplate" x:DataType="local:Result">
<StackPanel Orientation="Horizontal" Height="Auto" Padding="12" AutomationProperties.Name="{x:Bind ID}">
<TextBlock Text="{x:Bind ToString()}" VerticalAlignment="Center" Style="{ThemeResource BaseTextBlockStyle}" Foreground="White" Margin="12,0,0,0" FontSize="24"/>
</StackPanel>
</DataTemplate>
</ResourceDictionary>
这是我引用模板的方式:
<ListView Grid.Row="1" ItemsSource="{x:Bind VM.Results}" ItemTemplate="{StaticResource LVTemplate}" Background="#FF343434" >
</ListView>
当我将它放在列表视图模板而不是字典中时,它的外观如下:
<ListView Grid.Row="1" ItemsSource="{x:Bind VM.Results}" Background="#FF343434" >
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Result">
<StackPanel Orientation="Horizontal" Height="Auto" Padding="12" AutomationProperties.Name="{x:Bind ID}">
<TextBlock Text="{x:Bind ToString()}" VerticalAlignment="Center" Style="{ThemeResource BaseTextBlockStyle}" Foreground="White" Margin="12,0,0,0" FontSize="24"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
以下是它不在 ResourceDictionary 中时工作的屏幕截图:
这不起作用:
编辑: 这是我的 App.xaml:
<Application
x:Class="Marathon.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Marathon">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/VCenterTextBox.xaml"/>
<ResourceDictionary Source="Resources/KeypadButton.xaml"/>
<ResourceDictionary Source="Resources/ListViewTemplate.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
【问题讨论】:
-
您尝试使用 Get 添加一个与 ToString() 相同的属性怎么样?可能绑定到一个方法有一些问题,比如它被调用了,结果是空的,然后列表可能会被项目填充,但是一个方法没有被再次调用,所以没有结果。尝试编写您自己的属性,当有一些数据要显示时调用 PropertyChanged。只是我解决这个问题的另一种方法。