2012-01-25 73 views
0
MobileApp.Get().Resources.MergedDictionaries.Clear(); 
Uri uri = new Uri("/Resources/NightModeButton.xaml", UriKind.Relative); 
ResourceDictionary resDict = Application.LoadComponent(uri) as ResourceDictionary; 

int count = resDict.Values.Count; //count is 1 

foreach (object obj in resDict.Values) 
{ 
    Style style = obj as Style; //NOW I HAVE THE STYLE 

    //How do I modify the color of element named "GradientStop0" ??? 
} 
<!-- NightButtonMode.xaml --> 
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

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

           <VisualState x:Name="Normal"/> 
            <VisualState x:Name="MouseOver"> 
             <Storyboard> 
              <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="InnerRectangle"> 
               <EasingColorKeyFrame KeyTime="0" Value="#FF9B7C35"/> 
              </ColorAnimationUsingKeyFrames> 
              <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="InnerRectangle"> 
               <EasingColorKeyFrame KeyTime="0" Value="#FF837C35"/> 
              </ColorAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Pressed"/> 
            <VisualState x:Name="Disabled"/> 
           </VisualStateGroup> 

          </VisualStateManager.VisualStateGroups> 
          <Rectangle x:Name="InnerRectangle" Margin="8,8,8,8" RadiusY="10" RadiusX="10" StrokeThickness="5"> 
           <Rectangle.Effect> 
            <BlurEffect/> 
           </Rectangle.Effect> 
           <Rectangle.Stroke> 
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
             <GradientStop Color="#7F7E7E7E" Offset="1" /> 
             <GradientStop Color="#7F605F5F" x:Name="GradientStop0"/> 
            </LinearGradientBrush> 
           </Rectangle.Stroke> 
           <Rectangle.Fill> 
            <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0"> 
             <GradientStop Color="White" Offset="0.228"/> 
             <GradientStop Color="#FFB0BAA1"/> 
             <GradientStop Color="#FFB0BAA1" Offset="0.823"/> 
            </LinearGradientBrush> 
           </Rectangle.Fill> 
          </Rectangle> 
          <Rectangle x:Name="GlassCube" Margin="8,8,8,8" RadiusY="10" RadiusX="10" StrokeThickness="5" Opacity="1"> 
           <Rectangle.Stroke> 
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
             <GradientStop Color="#7FFFFFFF" /> 
             <GradientStop Color="#7FFFFFFF"/> 
             <GradientStop Color="#F2FFFFFF" Offset="0.254"/> 
             <GradientStop Color="#BFFFFFFF" Offset="0.401"/> 
            </LinearGradientBrush> 
           </Rectangle.Stroke> 
           <Rectangle.OpacityMask> 
            <RadialGradientBrush> 
             <GradientStop Color="#FFFBFBFB" Offset="0"/> 
             <GradientStop Color="White" Offset="1"/> 
            </RadialGradientBrush> 
           </Rectangle.OpacityMask> 
           <Rectangle.Fill> 
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
             <GradientStop Color="White" Offset="0"/> 
             <GradientStop Color="#FFFDFDFD"/> 
             <GradientStop Color="#7FFEFEFE"/> 
             <GradientStop Color="#FFFDFDFD"/> 
             <GradientStop Color="#7FFDFDFD" Offset="0.004"/> 
             <GradientStop Color="#FFFDFDFD"/> 
             <GradientStop Color="#7FFDFDFD"/> 
            </LinearGradientBrush> 
           </Rectangle.Fill> 
          </Rectangle> 
          <ContentPresenter x:Name="myContentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsCancel" Value="False"/> 
          <EventTrigger RoutedEvent="FrameworkElement.Loaded"/> 
          <EventTrigger RoutedEvent="FrameworkElement.Loaded"/> 
          <EventTrigger RoutedEvent="FrameworkElement.Loaded"/> 
          <Trigger Property="IsEnabled" Value="False"/> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

</ResourceDictionary> 

回答

2

你幾乎不能修改的模板部分,甚至沒有嘗試。

如果您需要改變某處的顏色,將其聲明爲資源並在需要時使用DynamicResource進行引用,如果您用另一種顏色替換它,則應更新所有內容。

例如

<Color x:Key="HighlightColor">Red</Color> 
<SolidColorBrush x:Key="HighlightBrush" Color="{DynamicResource HighlightColor}" /> 
<Border.Background> 
    <LinearGradientBrush> 
     <GradientStop Offset="0" Color="White" /> 
     <GradientStop Offset="1" Color="{DynamicResource HighlightColor}" /> 
    </LinearGradientBrush> 
</Border.Background> 
Resources["HighlightColor"] = Colors.Green; 
+0

所以我添加<的SolidColorBrush X:鍵= 「Brush1」 顏色= 「白」/>在頂部和替換{DynamicResource Brush1}的值,而是該崩潰。你能提供一些關於如何做到這一點的見解嗎? – 0x4f3759df

+0

@iterationx:您無法在漸變停止中引用畫筆,請參閱我的示例。 (在示例中未使用'HighlightBrush',但在那裏應該直接設置'Brush'屬性] –

+0

非常感謝... – 0x4f3759df