2011-09-28 35 views
0

我正在修改默認的「ChildWindow」樣式,我想指定與默認情況下存在的「X」不同的圖像。我已經嘗試過各種東西,比如鉤住OnApplyTemplate和OnOpened,在那裏我可以得到編程訪問按鈕,像這樣:更改ChildWindow CloseButton圖像Silverlight 3

Button closeButton = this.GetTemplateChild("CloseButton") as Button; 

然而closeButton.Content總是空。我已經嘗試將其設置爲我的圖像,它確實設置,但用戶界面仍然顯示默認的'X'。我也調用了UpdateLayout()以至於無濟於事。

有沒有辦法做到這一點無論是編程或通過XAML?我已經複製了默認樣式並進行了受影響的更改,但這是一個讓我難以接受的改變。下面是風格XAML我一直使用的一個片段:

<!-- Header with Title--> 
<Border x:Name="Chrome" Width="Auto" CornerRadius="5,5,0,0" Background="{StaticResource ChildWindowHeaderBackgroundBrush}"> 
    <Grid Height="Auto" Width="Auto"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition Width="30"/> 
     </Grid.ColumnDefinitions> 
     <ContentControl Content="{TemplateBinding Title}" 
      IsTabStop="False" 
      FontWeight="Bold" 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Center" 
      Margin="6,0,6,0"/>  
     <Button x:Name="CloseButton" Margin="0,5,0,5" Grid.Column="2" IsTabStop="False" HorizontalAlignment="Center" VerticalAlignment="Center" Width="15" Height="14" Style="{StaticResource ButtonStyle}"/> 
    </Grid> 
</Border> 

我已經通過XAML增加了圖像的按鈕,它仍然沒有露面。

回答

0

它通過樣式的所有處理,在控件模板:

<Style x:Key="ButtonStyle" TargetType="Button"> 
    <Setter Property="Padding" Value="0"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
         <VisualState x:Name="Disabled"/> 
         <VisualState x:Name="Normal"/> 
         <VisualState x:Name="Pressed"/> 
         <VisualState x:Name="MouseOver"> 
          <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="mouseOverImage"> 
           <DiscreteObjectKeyFrame KeyTime="0"> 
           <DiscreteObjectKeyFrame.Value> 
            <Visibility>Visible</Visibility> 
           </DiscreteObjectKeyFrame.Value> 
           </DiscreteObjectKeyFrame> 
          </ObjectAnimationUsingKeyFrames> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="normalImage"> 
           <DiscreteObjectKeyFrame KeyTime="0"> 
           <DiscreteObjectKeyFrame.Value> 
            <Visibility>Collapsed</Visibility> 
           </DiscreteObjectKeyFrame.Value> 
           </DiscreteObjectKeyFrame> 
          </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Image x:Name="normalImage" Margin="0" Source="/img/status_unknown.png" Stretch="None" /> 
        <Image x:Name="mouseOverImage" Margin="0" Source="/img/up.png" Stretch="None" Visibility="Collapsed" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>