代码之家  ›  专栏  ›  技术社区  ›  Will

如何正确实现样式触发器,以便在ListView完成填充时触发?

  •  0
  • Will  · 技术社区  · 7 年前

    我想建立我的 ListView 因此,一旦填充,外观将发生更改。我发现有一个叫做 ItemContainerGenerator 列表视图 包含 Status 将设置为的属性 ContainersGenerated 一旦这一切结束(我想)。

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

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

    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. 在主窗口上复制并粘贴以下代码。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>

    (不幸的是,因为它都在XAML中,所以我必须将整个代码块包装在`中才能显示出来)

    那么,我如何正确地创建一个数据触发器,当 列表视图 完成填充?

    1 回复  |  直到 7 年前
        1
  •  2
  •   SledgeHammer    7 年前

    删除ItemContainerGenerator周围的括号。地位对我有用。顺便说一句,如果你正在使用回收,你会在滚动时不断切换状态。如果您的列表需要很长时间才能填充,听起来您应该使用回收和虚拟化。对于“正常”数量的项目,用户甚至看不到未完成状态,因为它会立即发生。