我已经试着问了,但可能我没有提供足够的信息。我正在尝试创建我自己的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背景和BorderBrush时,TextBox被禁用。
颜色在相同的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的所有东西的颜色更改为红色”
2/“当我切换这些颜色时,仍然只有OniiControlBackgroundBrush被更改,但这次改为黄色”
<Rectangle x:Name="Background" Fill="{StaticResource OniiNormalBrush}" Stroke="{StaticResource OniiControlBackgroundBrush}" RadiusX="2" RadiusY="2"/>3/“一切都在一个资源字典中定义”
<Application.Resources>
<ResourceDictionary Source="Theme/OniiResourceDictionary.xaml">
</ResourceDictionary>
</Application.Resources>//“我尝试用较小的解决方案来重现这个问题,并且使用较少的自定义样式,但没有成功。”
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没有问题。谢谢。
发布于 2012-11-12 14:55:39
问题似乎在于您的资源绑定。使用StaticResource时,意味着在窗口初始化时加载资源,然后从此不再加载资源。因此,在大多数情况下,当您修改资源时,绑定到它的控件并不知道更改。
在#4中,您没有遇到这个问题,因为您使用了DynamicResource,它允许在运行时更新资源。
编辑:在第二次查看事物时,它看起来可能是两件事中的一件。
一..。颜色的静态绑定不允许更新。当将颜色绑定到您的刷子上时,从静态到动态的更改是否解决了问题?
<Color x:Key="MainColor">#FF595959</Color>
<Color x:Key="OniiControlBackgroundColor">#FF333333</Color>
<SolidColorBrush x:Key="OniiNormalBrush" Color="{DynamicResource MainColor}"/>
<SolidColorBrush x:Key="OniiControlBackgroundBrush" Color="{DynamicResource OniiControlBackgroundColor}" />二..。动画正在设置画笔上的颜色。它本质上是通过给绑定指定一个直接的颜色来清除绑定。当动画结束时,我会假设颜色会被重新设置为绑定,但情况可能并非如此。这是否有任何影响:
<SolidColorBrush x:Key="OniiNormalBrush" Color="#FF595959"/>
<SolidColorBrush x:Key="OniiControlBackgroundBrush" Color="#FF333333" />https://stackoverflow.com/questions/13344678
复制相似问题