应用程序样式时 WPF DataTrigger 未触发?

发布于 2024-08-19 02:11:10 字数 8191 浏览 19 评论 0原文

对于这个问题的任何帮助将不胜感激,因为我整天都在寻找有关该领域的答案!

我通过将合并字典添加到 App.xaml 来将全局样式应用于我的 WPF 应用程序。这已按预期在整个应用程序中应用了样式,但它所做的许多事情我并不完全理解。

如果有帮助的话,我可以给你该样式正在应用的代码,但它相当大,所以最好不要阻塞这篇文章。该样式将背景颜色应用于每个列表框项目,以及酷炫的悬停动画和颜色变化。不过,此样式并未应用于我的应用程序中的几个列表框,下面是其中一个的代码示例:

<StackPanel Margin="0,15,0,0" Width="auto" HorizontalAlignment="Left">
    <StackPanel.Resources>
        <converter:IntToBoolConverter x:Key="intToBoolConverter" />
        <converter:BoolToVisibilityConverter x:Key="boolToVisibilityConverter" />
    </StackPanel.Resources>
    <Label Content="Required Vehicles" HorizontalAlignment="Center" FontWeight="Bold" />
    <ListBox x:Name="lstVehicleRequests" ItemsSource="{Binding VehicleRequests}" Width="auto"
             IsSynchronizedWithCurrentItem="True">
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource BaseListBoxItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=RequestStatus.RequestStatusId}" Value="7">
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FFFFFFFB" Offset="0" />
                                    <GradientStop Color="IndianRed" Offset="0.5" />
                                    <GradientStop Color="#FFFFFFFB" Offset="1" />
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid ShowGridLines="True">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="4" />
                    </Grid.RowDefinitions>
                    <StackPanel Margin="0,8,0,0">
                        <TextBlock Margin="0,4,10,0" >
                        <Label Content="Coach Type" Width="120" />
                        <ComboBox ItemsSource="{Binding DataContext.CoachTypes, 
                                  RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                  SelectedItem="{Binding CoachType}" DisplayMemberPath="Name" Width="100" />
                        </TextBlock>
                        <TextBlock Margin="0,8,10,0">
                            <Label Content="No of Passengers" Width="120" />
                            <TextBox 
                                    keys:ValidKeys.Numeric="True"
                                    Validation.ErrorTemplate="{StaticResource validationTemplate}"
                                    Style="{StaticResource textBoxInError}" Width="50">
                                <TextBox.Text>
                                        <Binding Path="Passengers"
                                                  UpdateSourceTrigger="PropertyChanged">
                                            <Binding.ValidationRules>
                                                <val:RegularExpressionRule 
                                                    ErrorDescription="Please Enter a Numeric Size" 
                                                    RegularExpression="^\d*$" />
                                            </Binding.ValidationRules>
                                        </Binding>
                                    </TextBox.Text>
                            </TextBox>
                        </TextBlock>
                        <TextBlock Margin="0,8,10,0">
                            <Label Content="No of Drivers" Width="120" />
                            <TextBox
                                    keys:ValidKeys.Numeric="True"
                                    Validation.ErrorTemplate="{StaticResource validationTemplate}"
                                    Style="{StaticResource textBoxInError}" Width="50">
                                <TextBox.Text>
                                    <Binding Path="Drivers"
                                        UpdateSourceTrigger="PropertyChanged">
                                        <Binding.ValidationRules>
                                            <val:RegularExpressionRule 
                                                ErrorDescription="Please Enter a Numeric Size" 
                                                RegularExpression="^\d*$" />
                                            </Binding.ValidationRules>
                                        </Binding>
                                    </TextBox.Text>
                            </TextBox>
                        </TextBlock>
                        <TextBlock Margin="0,8,10,0">
                        <Label Content="Positioning Feeder Drivers" Width="120" />
                            <TextBox
                                    keys:ValidKeys.Numeric="True"
                                    Style="{StaticResource textBoxInError}" Width="50" MaxLength="3">
                                <TextBox.Text>
                                    <Binding Path="PositioningFeederDrivers"
                                        UpdateSourceTrigger="PropertyChanged">
                                        <Binding.ValidationRules>
                                            <val:RegularExpressionRule 
                                                ErrorDescription="Please Enter a Numeric Size" 
                                                RegularExpression="^\d*$" />
                                            </Binding.ValidationRules>
                                        </Binding>
                                    </TextBox.Text>
                            </TextBox>
                        </TextBlock>
                        <TextBlock Margin="0,8,10,0">                    
                        <Label Content="Wheelchair Access" Width="120" />
                        <ComboBox Width="100" SelectedIndex="{Binding WheelchairAccess, 
                                  Converter={StaticResource intToBoolConverter}}">
                            <ComboBoxItem Content="Not Required" />
                            <ComboBoxItem Content="Required" />
                        </ComboBox>
                        </TextBlock>
                        <TextBlock Margin="0,8,10,8">
                        <Label Content="Trailer" Width="120" />
                        <ComboBox Width="100" SelectedIndex="{Binding Trailer, 
                                  Converter={StaticResource intToBoolConverter}}">
                            <ComboBoxItem Content="Not Required" />
                            <ComboBoxItem Content="Required" />
                        </ComboBox>
                        </TextBlock>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

然后我添加了 BasedOn 属性,如上所示,为什么我必须这样做才能将样式应用于此列表框?其他列表框和不同的控件会自动选择它吗?

不管怎样,你会看到我有一个针对此列表框的数据触发器,当请求状态 id = 7 时,它应该更改背景颜色。如果没有 based on 属性,该行会成功将颜色更改为红色。应用样式后,它不会改变颜色,并且始终应用模板中的橙色。

帮助?????

非常感谢,

马克

Any help on this problem would be greatly appreciated as I have been looking around all day for answers surrounding this area!

I have applied a global style to my WPF application by adding in a merged dictionary to the App.xaml. This has applied the style across the application like intended but there are a number of things that it has done that I do not fully understand.

I can give you the code that the style is applying if that will help but it is quite large so though it best not to clog this post. The style applies a background colour to each of the listbox items as well as cool hover over animations and colour changes. This style was not applied to a couple of the listboxes in my application though, code example of one below:

<StackPanel Margin="0,15,0,0" Width="auto" HorizontalAlignment="Left">
    <StackPanel.Resources>
        <converter:IntToBoolConverter x:Key="intToBoolConverter" />
        <converter:BoolToVisibilityConverter x:Key="boolToVisibilityConverter" />
    </StackPanel.Resources>
    <Label Content="Required Vehicles" HorizontalAlignment="Center" FontWeight="Bold" />
    <ListBox x:Name="lstVehicleRequests" ItemsSource="{Binding VehicleRequests}" Width="auto"
             IsSynchronizedWithCurrentItem="True">
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource BaseListBoxItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=RequestStatus.RequestStatusId}" Value="7">
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FFFFFFFB" Offset="0" />
                                    <GradientStop Color="IndianRed" Offset="0.5" />
                                    <GradientStop Color="#FFFFFFFB" Offset="1" />
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid ShowGridLines="True">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="4" />
                    </Grid.RowDefinitions>
                    <StackPanel Margin="0,8,0,0">
                        <TextBlock Margin="0,4,10,0" >
                        <Label Content="Coach Type" Width="120" />
                        <ComboBox ItemsSource="{Binding DataContext.CoachTypes, 
                                  RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                  SelectedItem="{Binding CoachType}" DisplayMemberPath="Name" Width="100" />
                        </TextBlock>
                        <TextBlock Margin="0,8,10,0">
                            <Label Content="No of Passengers" Width="120" />
                            <TextBox 
                                    keys:ValidKeys.Numeric="True"
                                    Validation.ErrorTemplate="{StaticResource validationTemplate}"
                                    Style="{StaticResource textBoxInError}" Width="50">
                                <TextBox.Text>
                                        <Binding Path="Passengers"
                                                  UpdateSourceTrigger="PropertyChanged">
                                            <Binding.ValidationRules>
                                                <val:RegularExpressionRule 
                                                    ErrorDescription="Please Enter a Numeric Size" 
                                                    RegularExpression="^\d*$" />
                                            </Binding.ValidationRules>
                                        </Binding>
                                    </TextBox.Text>
                            </TextBox>
                        </TextBlock>
                        <TextBlock Margin="0,8,10,0">
                            <Label Content="No of Drivers" Width="120" />
                            <TextBox
                                    keys:ValidKeys.Numeric="True"
                                    Validation.ErrorTemplate="{StaticResource validationTemplate}"
                                    Style="{StaticResource textBoxInError}" Width="50">
                                <TextBox.Text>
                                    <Binding Path="Drivers"
                                        UpdateSourceTrigger="PropertyChanged">
                                        <Binding.ValidationRules>
                                            <val:RegularExpressionRule 
                                                ErrorDescription="Please Enter a Numeric Size" 
                                                RegularExpression="^\d*$" />
                                            </Binding.ValidationRules>
                                        </Binding>
                                    </TextBox.Text>
                            </TextBox>
                        </TextBlock>
                        <TextBlock Margin="0,8,10,0">
                        <Label Content="Positioning Feeder Drivers" Width="120" />
                            <TextBox
                                    keys:ValidKeys.Numeric="True"
                                    Style="{StaticResource textBoxInError}" Width="50" MaxLength="3">
                                <TextBox.Text>
                                    <Binding Path="PositioningFeederDrivers"
                                        UpdateSourceTrigger="PropertyChanged">
                                        <Binding.ValidationRules>
                                            <val:RegularExpressionRule 
                                                ErrorDescription="Please Enter a Numeric Size" 
                                                RegularExpression="^\d*$" />
                                            </Binding.ValidationRules>
                                        </Binding>
                                    </TextBox.Text>
                            </TextBox>
                        </TextBlock>
                        <TextBlock Margin="0,8,10,0">                    
                        <Label Content="Wheelchair Access" Width="120" />
                        <ComboBox Width="100" SelectedIndex="{Binding WheelchairAccess, 
                                  Converter={StaticResource intToBoolConverter}}">
                            <ComboBoxItem Content="Not Required" />
                            <ComboBoxItem Content="Required" />
                        </ComboBox>
                        </TextBlock>
                        <TextBlock Margin="0,8,10,8">
                        <Label Content="Trailer" Width="120" />
                        <ComboBox Width="100" SelectedIndex="{Binding Trailer, 
                                  Converter={StaticResource intToBoolConverter}}">
                            <ComboBoxItem Content="Not Required" />
                            <ComboBoxItem Content="Required" />
                        </ComboBox>
                        </TextBlock>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

I then added the BasedOn property as you can see above, why did I have to do this to get the style applied to this list box? Other listboxes and different controls picked this up automatically?

Anyway, you will see that I have a datatrigger against this listbox which should change the backround colour when the request status id = 7. Without the based on property the row successfully changes colour to red. When the style is applied it never changes colour and the orange colour from the template is always applied.

HELP?????

Much appreciated,

Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂ガ小子 2024-08-26 02:11:10
<Style x:Key="BaseListBoxItem" d:IsControlPart="True" TargetType="{x:Type ListBoxItem}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="Padding" Value="3" />
        <Setter Property="Foreground" Value="{StaticResource OutsideFontColor}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="HoverOn">
                            <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientOver" 
                                             Storyboard.TargetProperty="Opacity" To="0.73" />
                        </Storyboard>
                        <Storyboard x:Key="HoverOff">
                            <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientOver" 
                                             Storyboard.TargetProperty="Opacity" To="0" />
                        </Storyboard>
                        <Storyboard x:Key="SelectedOn">
                            <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientSelected" 
                                             Storyboard.TargetProperty="Opacity" To="0.84" />
                            <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientSelectedDisabled" 
                                             Storyboard.TargetProperty="Opacity" To="0.55" />
                        </Storyboard>
                        <Storyboard x:Key="SelectedOff">
                            <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientSelected" 
                                             Storyboard.TargetProperty="Opacity" To="0" />
                            <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientSelectedDisabled" 
                                             Storyboard.TargetProperty="Opacity" To="0" />
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Grid SnapsToDevicePixels="true">
                        <Rectangle x:Name="BackgroundGradientOver" RadiusX="1" RadiusY="1" Stroke="{DynamicResource MouseOverBorderBrush}" 
                                   Opacity="0" Fill="{DynamicResource MouseOverBrush}"/>
                        <Rectangle x:Name="BackgroundGradientSelectedDisabled" RadiusX="1" RadiusY="1" Opacity="0" Fill="{DynamicResource 
                            ListItemSelectedBrush}" Stroke="{DynamicResource ListItemSelectedBorderBrush}"/>
                        <Rectangle x:Name="BackgroundGradientSelected" Stroke="{DynamicResource PressedBorderBrush}" StrokeThickness="1" 
                                   RadiusX="1" RadiusY="1" Opacity="0" Fill="{DynamicResource PressedBrush}">

                        </Rectangle>
                        <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}" 
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  />
                    </Grid>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsSelected" Value="true">
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource SelectedOff}" x:Name="SelectedOff_BeginStoryboard" />
                            </Trigger.ExitActions>
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource SelectedOn}" x:Name="SelectedOn_BeginStoryboard" />
                            </Trigger.EnterActions>

                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource HoverOff}" x:Name="HoverOff_BeginStoryboard" />
                            </Trigger.ExitActions>
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource HoverOn}" />
                            </Trigger.EnterActions>
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
<Style x:Key="BaseListBoxItem" d:IsControlPart="True" TargetType="{x:Type ListBoxItem}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="Padding" Value="3" />
        <Setter Property="Foreground" Value="{StaticResource OutsideFontColor}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="HoverOn">
                            <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientOver" 
                                             Storyboard.TargetProperty="Opacity" To="0.73" />
                        </Storyboard>
                        <Storyboard x:Key="HoverOff">
                            <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientOver" 
                                             Storyboard.TargetProperty="Opacity" To="0" />
                        </Storyboard>
                        <Storyboard x:Key="SelectedOn">
                            <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientSelected" 
                                             Storyboard.TargetProperty="Opacity" To="0.84" />
                            <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientSelectedDisabled" 
                                             Storyboard.TargetProperty="Opacity" To="0.55" />
                        </Storyboard>
                        <Storyboard x:Key="SelectedOff">
                            <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientSelected" 
                                             Storyboard.TargetProperty="Opacity" To="0" />
                            <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientSelectedDisabled" 
                                             Storyboard.TargetProperty="Opacity" To="0" />
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Grid SnapsToDevicePixels="true">
                        <Rectangle x:Name="BackgroundGradientOver" RadiusX="1" RadiusY="1" Stroke="{DynamicResource MouseOverBorderBrush}" 
                                   Opacity="0" Fill="{DynamicResource MouseOverBrush}"/>
                        <Rectangle x:Name="BackgroundGradientSelectedDisabled" RadiusX="1" RadiusY="1" Opacity="0" Fill="{DynamicResource 
                            ListItemSelectedBrush}" Stroke="{DynamicResource ListItemSelectedBorderBrush}"/>
                        <Rectangle x:Name="BackgroundGradientSelected" Stroke="{DynamicResource PressedBorderBrush}" StrokeThickness="1" 
                                   RadiusX="1" RadiusY="1" Opacity="0" Fill="{DynamicResource PressedBrush}">

                        </Rectangle>
                        <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}" 
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  />
                    </Grid>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsSelected" Value="true">
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource SelectedOff}" x:Name="SelectedOff_BeginStoryboard" />
                            </Trigger.ExitActions>
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource SelectedOn}" x:Name="SelectedOn_BeginStoryboard" />
                            </Trigger.EnterActions>

                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource HoverOff}" x:Name="HoverOff_BeginStoryboard" />
                            </Trigger.ExitActions>
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource HoverOn}" />
                            </Trigger.EnterActions>
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文