[英]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.