C# 样式触发器属性未更改元素';s宽度(以XAML为单位)

C# 样式触发器属性未更改元素';s宽度(以XAML为单位),c#,wpf,xaml,C#,Wpf,Xaml,我试图触发元素的IsMouseOver属性,并在XAML中更改宽度,但问题是如果我在元素的标记中定义默认或静态宽度属性,那么这就不起作用。为什么? 工作代码 <UserControl x:Class="IntelliVentory.UserControls.SideMenuBarControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http:

我试图触发元素的
IsMouseOver
属性,并在XAML中更改宽度,但问题是如果我在元素的标记中定义默认或静态宽度属性,那么这就不起作用。为什么?
工作代码

<UserControl x:Class="IntelliVentory.UserControls.SideMenuBarControl"
         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:IntelliVentory.UserControls"
         mc:Ignorable="d"
         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         xmlns:userControls="clr-namespace:IntelliVentory.UserControls"
         TextElement.Foreground="{DynamicResource MaterialDesignBody}"
         Background="{DynamicResource MaterialDesignPaper}"
         TextElement.FontWeight="Medium"
         TextElement.FontSize="14"
         FontFamily="pack://application:,,,/MaterialDesignThemes.Wpf;component/Resources/Roboto/#Roboto"
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>
    <ItemsControl Margin="50">
        <ItemsControl.Resources>
            <Style x:Key="ScaleStyle" TargetType="materialDesign:ColorZone">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Width" Value="300" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ItemsControl.Resources>
        <materialDesign:ColorZone Name="ColorZone1" Style="{StaticResource ScaleStyle}" Height="50"
                                  Mode="PrimaryDark" Padding="16" CornerRadius="3"
                                  materialDesign:ShadowAssist.ShadowDepth="Depth3" />
        <materialDesign:ColorZone Margin="0 5 0 0" Name="ColorZone2" Style="{StaticResource ScaleStyle}"
                                  Height="50" Mode="PrimaryDark" Padding="16" CornerRadius="3"
                                  materialDesign:ShadowAssist.ShadowDepth="Depth3" />
        <materialDesign:ColorZone Margin="0 5 0 0" Name="ColorZone3" Style="{StaticResource ScaleStyle}"
                                  Height="50" Mode="PrimaryDark" Padding="16" CornerRadius="3"
                                  materialDesign:ShadowAssist.ShadowDepth="Depth3" />

    </ItemsControl>
</Grid>

您需要在样式中设置默认宽度

        <Style x:Key="ScaleStyle" TargetType="materialDesign:ColorZone">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Width" Value="300" />
                </Trigger>
            </Style.Triggers>
             <Style.Setters>
                <Setter Property="Width" Value="40"/>
             </Style.Setters>
        </Style>


由于依赖项属性值的优先级,属性
Width=“40”
将覆盖样式所做的任何操作。如果要设置默认宽度,请在触发器之外的另一个样式设置器中设置它。@EdPlunkett好的,我现在明白了。但是您能告诉我如何解决这个问题吗!应该感谢:)非常感谢!还有一件事。此触发器属性正在应用于项控件内的所有元素,但不希望如此,因此我可以提取我不希望应用此属性的其他元素!但是还有别的办法吗?所以。。我可以将此触发器属性应用于我想要的ColorZone的任何元素??我可以使此样式全局可用,以便我可以将其用于我想要的任何ColorZone元素!换言之,以下是您关于如何在全球范围内应用样式的答案。
        <Style x:Key="ScaleStyle" TargetType="materialDesign:ColorZone">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Width" Value="300" />
                </Trigger>
            </Style.Triggers>
             <Style.Setters>
                <Setter Property="Width" Value="40"/>
             </Style.Setters>
        </Style>