c# - WPF 如何将 DataTrigger 应用到 XAML 中的自定义用户控件类

标签 c# wpf xaml datatrigger

我遇到以下问题。我有一个自定义用户控件,它基本上是一个非常自定义的 ListView,它具有各种排序和过滤器,我在桌面应用程序中多次使用它。它的 XAML 看起来像这样

<UserControl x:Class="ReportLib.FilteredList"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ReportLib"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="MainGrid">
    <ListView x:Name="lvwData" GridViewColumnHeader.Click="lvwHeaderClick" SizeChanged="lvwData_SizeChanged">
        <ListView.ContextMenu>
            <ContextMenu>
                <MenuItem x:Name="mnuExport" Header="Export to Excel" Click="mnuExport_Click"/>
            </ContextMenu>
        </ListView.ContextMenu>
    </ListView>
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="waitGrid" Visibility="Hidden">
        <Grid.Effect>
            <DropShadowEffect></DropShadowEffect>
        </Grid.Effect>
        <Rectangle RadiusX="5" RadiusY="5" Stroke="Black" StrokeThickness="1" Fill="White"/>
        <StackPanel Margin="2" Orientation="Horizontal" >
            <MediaElement Source="waiting.gif" Stretch="UniformToFill" Width="35" Height="25" VerticalAlignment="Center" Margin="10" Volume="0"/>
            <TextBlock Text="Please wait" FontSize="24" Margin="0,0,20, 0" VerticalAlignment="Center" HorizontalAlignment="Center" />
        </StackPanel>
    </Grid>
</Grid>

我在其他 XAML/其他项目中使用它,就像这样

<ReportLib:FilteredList x:Name="myCustomReportList">

我的问题是,现在我已经制作了一个实时更新的动态网格,我需要对其应用 DataTrigger,但我无法让它工作。

到目前为止,我的这个基于研究和失败的尝试

<ReportLib:FilteredList x:Name="lstSessions">
                    <Style TargetType="{x:Type ReportLib:FilteredList}" BasedOn="{StaticResource {x:Type UserControl}}">
                        <Style.Resources>
                            <Style TargetType="{x:Type ListViewItem}">
                                <Style.Triggers>
                                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                        <Setter Property="Background" Value="Black" />
                                    </Trigger>
                                    <DataTrigger Binding="{Binding HasError}" Value="True">
                                        <Setter Property="FontWeight" Value="Bold" />
                                        <Setter Property="Foreground" Value="White"/>
                                        <Setter Property="Background" Value="Red" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Style.Resources>
                    </Style>
                </ReportLib:FilteredList>

但它只在网格中显示System.Windows.Style,并且不显示实际的行。如果我不做任何时尚的 shannanigans 一切都会显示/正常工作。我正在寻找任何建议。

提前致谢。

最佳答案

您错过了 Style 属性标记,因此 Style 元素被识别为列表项。

<ReportLib:FilteredList x:Name="lstSessions">
    <ReportLib:FilteredList.Style>
        <Style TargetType="ReportLib:FilteredList" ...>
            ...
        </Style>
    </ReportLib:FilteredList.Style>
</ReportLib:FilteredList>

关于c# - WPF 如何将 DataTrigger 应用到 XAML 中的自定义用户控件类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48205057/

相关文章:

wpf - DomainCollectionView单元测试

WPF - 选项卡中的 xaml 滚动条

wpf - 在 WPF 中的单个 GeometryDrawing 中使用多个画笔

c# - mvvmcross 导航问题,出现异常 : Could not find view for [ViewModelName]

c# - 使用 ASP.NET MVC 分页在网页上显示日志文件信息

c# - 如何在运行时在 web 表单 asp.net 上从 gridview 获取数据

wpf - 根据数据绑定(bind) ItemsSource 项中的值有条件地更改 WPF ComboBox 项的前景

c# - 如何修复 Activator.CreateInstance 因 MissingMethodException "Constructor on type not found"而失败?

c# - 在 Unity Inspector 中隐藏 EditorGUILayout.ObjectField 的 "Object Picker"?

wpf - window.Owner(预期行为)