繁体   English   中英

WPF如何将DataTrigger应用于XAML中的“自定义用户控件”类

[英]WPF how to apply DataTrigger to Custom User Control class in a XAML

我有以下问题。 我有一个自定义用户控件,它基本上是一个非常自定义的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,并且未显示实际行。 如果我不做任何时尚的装饰,一切都将正确显示/正常工作。 我正在寻找任何建议。

提前致谢。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM