简体   繁体   中英

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

I have the following problem. I have a custom user control that is basically a very customised ListView which has all kinds of sorts and filters, which I use multiple times in a desktop application. It's XAML looks like this

<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>

I use it in other XAMLs/other projects like so

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

My problem is that right now I have made a dynamic grid which is updated in real time and I need to apply a DataTrigger to it, but I cannot for the live of me make it work.

So far I have this based on research and failed tries

<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>

but it only displays System.Windows.Style in the grid and the actual rows are not shown. If I don't do any stylish shannanigans everything is displayed/works properly. I am looking for any suggestions.

Thanks in advance.

You've missed the Style property tag, so the Style element is recognized as a list item.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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