当ListView完成填充时,如何正确实现触发的样式触发器?

时间:2018-03-10 23:10:10

标签: c# wpf xaml listview datatrigger

我想设置ListView的样式,以便在填充后外观会发生变化。我发现在ItemContainerGenerator中有一个名为ListView的内容包含Status属性,一旦完成(我认为),它将被设置为ContainersGenerated。< / p>

因此,为了实现这一目标,我认为最好的方法是定义一个样式数据触发器以触发该属性。

但它不起作用(当然)。我所看到的是输出中的以下错误 -

System.Windows.Data Error: 17 : Cannot get 'Status' value (type 'GeneratorStatus') from '' (type 'ListView'). BindingExpression:Path=(ItemContainerGenerator.Status); DataItem='ListView' (Name=''); target element is 'ListView' (Name=''); target property is 'NoTarget' (type 'Object') TargetException:'System.Reflection.TargetException: Object does not match target type. at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level) at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'

在提问时符合Minimal, Complete and Verifiable Example要求 -

  1. 在Visual Studio中创建一个名为“MCVE”的新项目。
  2. 将以下代码复制并粘贴到MainWindow.xaml代码上。
  3. 运行程序。获取数据错误。默默地哭泣。

    <Window x:Class="MCVE.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MCVE" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <ListView> <ListView.Style> <Style TargetType="{x:Type ListView}"> <Style.Triggers> <DataTrigger Binding="{Binding (ItemContainerGenerator.Status), RelativeSource={RelativeSource Self}}" Value="{x:Static GeneratorStatus.ContainersGenerated}"> <!-- This is where I'd put my setters - IF THE TRIGGER WORKED!!! --> <Setter Property="Background" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> </ListView.Style> <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Margin" Value="0" /> <Setter Property="Padding" Value="0" /> </Style> </ListView.ItemContainerStyle> <ListView.ItemsPanel> <ItemsPanelTemplate> <UniformGrid HorizontalAlignment="Center" Rows="1" /> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListViewItem>Foo</ListViewItem> <ListViewItem>Bar</ListViewItem> <ListViewItem>Baz</ListViewItem> </ListView> </Window>

  4. (不幸的是因为它全部在XAML中,我必须将整个代码块包装在`以使其显示)

    那么如何正确创建一个数据触发器,当ListView完成填充时会触发?

1 个答案:

答案 0 :(得分:1)

删除ItemContainerGenerator.Status周围的括号。适合我。顺便说一句,如果你正在使用回收,你滚动时会不断切换状态。听起来你应该使用回收和虚拟化,如果你的列表需要很长时间才能填充。正常&#34;正常&#34;物品数量,用户甚至不会看到未完成的状态,因为它会立即发生。

相关问题