我已经尝试过询问,但可能我没有提供足够的信息。我正在尝试创建自己的 WPF 主题。一切都很好,直到我创造了这种风格。
<Style TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PART_ContentHost">
<EasingDoubleKeyFrame KeyTime="0" Value="0.5"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Background">
<EasingColorKeyFrame KeyTime="0" Value="Red"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="Background">
<EasingColorKeyFrame KeyTime="0" Value="Yellow"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="ReadOnly"/>
<VisualState x:Name="MouseOver"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="Background" Fill="{StaticResource OniiControlBackgroundBrush}" Stroke="{StaticResource OniiNormalBrush}" RadiusX="2" RadiusY="2"/>
<ScrollViewer x:Name="PART_ContentHost" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" FontFamily="{TemplateBinding FontFamily}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
</Style>
当 TextBox 被禁用时,它应该改变 TextBox Background 和 BorderBrush 的颜色。
颜色在同一个 ResourceDictionary 中定义
<Color x:Key="MainColor">#FF595959</Color>
<Color x:Key="OniiControlBackgroundColor">#FF333333</Color>
<SolidColorBrush x:Key="OniiNormalBrush" Color="{StaticResource MainColor}"/>
<SolidColorBrush x:Key="OniiControlBackgroundBrush" Color="{StaticResource OniiControlBackgroundColor}" />
我不知道我真正的问题是什么。我知道的:
1/“当 TextBox 被禁用时,它会将所有使用 OniiControlBackgroundBrush 的颜色更改为红色”
- OniiControlBackgroundBrush 在其他样式中被引用为 StaticResource
2/“当我切换这些颜色时,仍然只有 OniiControlBackgroundBrush 被更改,但这次变为黄色”
<Rectangle x:Name="Background" Fill="{StaticResource OniiNormalBrush}" Stroke="{StaticResource OniiControlBackgroundBrush}" RadiusX="2" RadiusY="2"/>
- OniiNormalBrush 在其他样式中也被引用为 StaticResource
3/ “一切都在一个资源字典中定义”
<Application.Resources>
<ResourceDictionary Source="Theme/OniiResourceDictionary.xaml">
</ResourceDictionary>
</Application.Resources>
4/“我尝试在较小的解决方案中使用较少的自定义样式重现此问题,但我没有成功”
我使用了相同的 TextBox 样式。
<TextBox Height="32" HorizontalAlignment="Left" Margin="38,51,0,0" Name="textBox1" VerticalAlignment="Top" Width="215" /> <CheckBox Content="Enabled" Height="16" HorizontalAlignment="Left" Margin="259,51,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_Unchecked" /> <Border Height="148" HorizontalAlignment="Left" Margin="254,126,0,0" Name="border1" VerticalAlignment="Top" Width="98" /> <Rectangle Fill="{StaticResource OniiNormalBrush}" StrokeThickness="20" Stroke="{StaticResource OniiControlBackgroundBrush}" Height="148" HorizontalAlignment="Left" Margin="358,126,0,0" Name="rectangle5" VerticalAlignment="Top" Width="99" />
文本框被 CheckBox 禁用和启用,Border 使用自定义样式,两种颜色都作为 StaticResources
5/“当我在原始解决方案中添加以下代码时,问题消失了”
<Rectangle Height="71" HorizontalAlignment="Left" Margin="130,131,0,0" Name="rectangle2" StrokeThickness="20" Stroke="{StaticResource OniiControlBackgroundBrush}" Fill="{StaticResource OniiNormalBrush}" VerticalAlignment="Top" Width="98" />
我真的错过了什么吗?还是我真的很愚蠢?无论如何,您的帮助将不胜感激。我真的迷路了。好吧,最后一件事我不想使用 x:Shared="false" 主要是因为我看到更改 OniiNormalBrush 没有问题。谢谢你。