通过在xaml中设置属性来动态更改边框背景

时间:2012-01-26 17:35:01

标签: wpf xaml

我正在尝试动态更改名为“Brush2”的边框背景属性,但是我收到以下错误。

  

无法将类型为“System.Windows.Media.Color”的对象强制转换为类型   'System.Windows.Media.Brush'。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Color x:Key="Brush0">#7F605F5F</Color>
    <Color x:Key="Brush1">#7F7E7E7E</Color>

    <Color x:Key="Color2">#7FFDFDFD</Color>
    <SolidColorBrush  x:Key="Brush2" Color="{DynamicResource Color2}" />

    <Color x:Key="Brush3">#7FFFFFFF</Color>
    <Color x:Key="Brush4">#BFFFFFFF</Color>
    <Color x:Key="Brush5">#F2FFFFFF</Color>
    <Color x:Key="Brush6">#FF6096E5</Color>
    <Color x:Key="Brush7">#FF9B7C35</Color>
    <Color x:Key="Brush8">#FFB0BAA1</Color>
    <Color x:Key="Brush9">#7F60605F</Color>
    <Color x:Key="Brush10">White</Color>



    <Style TargetType="{x:Type TabItem}" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>


                            <VisualStateGroup x:Name="CommonStates">
                                <!-- silver -->
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush8}"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush8}"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush0}"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush9}"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>


                                <VisualState x:Name="Normal"/>


                                <VisualState x:Name="MouseOver">
                                    <!-- bronze -->
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush7}"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush7}"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush0}"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
                                            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush9}"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>



                                <VisualState x:Name="Flashing">
                                    <Storyboard>
                                        <DoubleAnimation 
                                            Storyboard.TargetName="BorderGlass"
                                            Storyboard.TargetProperty="Opacity"
                                            From="0.0"
                                            To="1.0"                     
                                            Duration="0:0:0.33"
                                            AutoReverse="True"
                                            RepeatBehavior="Forever"/>
                                    </Storyboard>
                                </VisualState>


                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>


                        </VisualStateManager.VisualStateGroups>

                        <Border x:Name="border" BorderThickness="3" CornerRadius="5,5,0,0">
                            <Border.BorderBrush>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource Brush0}" Offset="0"/>
                                    <GradientStop Color="{DynamicResource Brush1}" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.BorderBrush>
                            <Border.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource Brush6}" Offset="0.004"/>
                                    <GradientStop Color="{DynamicResource Brush6}" Offset="0.823"/>
                                    <GradientStop Color="{DynamicResource Brush10}" Offset="0.228"/>
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                        <Border x:Name="BorderGlass" BorderThickness="3" CornerRadius="5,5,0,0" Background="{DynamicResource Brush2}">
                            <Border.BorderBrush>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource Brush3}" Offset="0"/>
                                    <GradientStop Color="{DynamicResource Brush4}" Offset="0.401"/>
                                    <GradientStop Color="{DynamicResource Brush5}" Offset="0.254"/>
                                </LinearGradientBrush>
                            </Border.BorderBrush>
                        </Border>

                        <ContentPresenter HorizontalAlignment="Center" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" ContentSource="Header"/>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>



</ResourceDictionary>

2 个答案:

答案 0 :(得分:0)

您需要做的是将画笔设置为solidcolorbrush对象。 http://msdn.microsoft.com/en-us/library/system.windows.media.solidcolorbrush.aspx

构造函数采用颜色对象。

答案 1 :(得分:0)

以下代码有效,但我必须这样做:

Uri uri2 = new Uri("/Resources/DayModeTab2.xaml", UriKind.Relative);
ResourceDictionary resDict2 = Application.LoadComponent(uri2) as ResourceDictionary;

//Make some changes
MobileApp.Get().Resources.MergedDictionaries.Add(resDict2);

而且我的糟糕命名方案也导致了冲突,因为我在2个xaml文件中使用了Brush0。

<Border x:Name="BorderGlass" BorderThickness="3" CornerRadius="5,5,0,0" >
  <Border.Background>
    <SolidColorBrush Color="{DynamicResource Color2}" />
   </Border.Background>
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
  <GradientStop Color="{DynamicResource Brush3}" Offset="0"/>
  <GradientStop Color="{DynamicResource Brush4}" Offset="0.401"/>
  <GradientStop Color="{DynamicResource Brush5}" Offset="0.254"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>