2011-07-13 58 views
2

我有下面的模板設置更改的ListView標題列HigghlightBackground顏色在運行時

<LinearGradientBrush x:Key="BorderBrush" StartPoint="0,0" EndPoint="0,1"> 
    <LinearGradientBrush.GradientStops> 
     <GradientStopCollection> 
      <GradientStop Color="White" Offset="0"/> 
      <GradientStop Color="White" Offset="1"/> 
     </GradientStopCollection> 
    </LinearGradientBrush.GradientStops> 
</LinearGradientBrush> 

<LinearGradientBrush x:Key="PressedBorderBrush" StartPoint="0,0" EndPoint="0,1"> 
    <LinearGradientBrush.GradientStops> 
     <GradientStopCollection> 
      <GradientStop Color="White" Offset="0"/> 
      <GradientStop Color="White" Offset="1"/> 
     </GradientStopCollection> 
    </LinearGradientBrush.GradientStops> 
</LinearGradientBrush> 

<Style x:Key="GridViewColumnHeaderGripper" TargetType="Thumb"> 
    <Setter Property="Width" Value="18"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Thumb}"> 
       <Border Padding="{TemplateBinding Padding}" Background="Transparent"> 
        <Rectangle HorizontalAlignment="Center" Width="1" Fill="{TemplateBinding Background}"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="{x:Type GridViewColumnHeader}" TargetType="GridViewColumnHeader"> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Bottom"/> 
    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=HeaderForeground, Converter={StaticResource ColorToBrushConverter}}"/> 
    <Setter Property="FontWeight" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=HeaderFontWeight}" /> 

    <Setter Property="Padding" Value="4"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="GridViewColumnHeader"> 
       <Grid> 
        <Border Name="HeaderBorder" Padding="{TemplateBinding Padding}" BorderThickness="0,0,0,0" BorderBrush="{StaticResource BorderBrush}" Background="{StaticResource BackgroundBrush}"> 
         <ContentPresenter Name="HeaderContent" Margin="0,0,0,0" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </Border> 
        <Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Margin="0,0,-9,0" Style="{StaticResource GridViewColumnHeaderGripper}" Cursor="ScrollSE"/> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="true"> 
         <Setter TargetName="HeaderBorder" Property="Background" Value="{StaticResource HighlightBackgroundBrush}"/> 
         <Setter TargetName="HeaderBorder" Property="CornerRadius" Value="2"/> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="true"> 
         <Setter TargetName="HeaderBorder" Property="Background" Value="{StaticResource PressedBorderBrush}"/> 
         <Setter TargetName="HeaderContent" Property="Margin" Value="0,0,0,0"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="Role" Value="Floating"> 
      <Setter Property="Opacity" Value="0.7"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="GridViewColumnHeader"> 
         <Canvas Name="PART_FloatingHeaderCanvas"> 
          <Rectangle Fill="#60000000" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}"/> 
         </Canvas> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
     <Trigger Property="Role" Value="Padding"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="GridViewColumnHeader"> 
         <Border Name="HeaderBorder" BorderThickness="0,0,0,0" BorderBrush="{StaticResource BorderBrush}" Background="{StaticResource BackgroundBrush}"/> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

在我codebehind在DataContextChanged事件期間我試圖改變標題列bakcground屬性。使用下面的邏輯,我能夠改變BackgroundBrush和BorderBrush

var backgroundBrush = FindResource("BackgroundBrush") as LinearGradientBrush; 
     if (backgroundBrush != null) { 
      if (backgroundBrush.GradientStops[0].Color != Color.FromArgb(255, 195, 193, 194)) { 
       backgroundBrush.GradientStops[0].Color = Color.FromArgb(255, 195, 193, 194); 
       backgroundBrush.GradientStops[1].Color = Color.FromArgb(255, 157, 157, 157); 
      } 
     } 

然而,當我嘗試做一些simular調整HighlightBackgroundBrush

  var highlightBrush = FindResource("HighlightBackgroundBrush") as LinearGradientBrush; 
     if (highlightBrush != null) { 
      if (highlightBrush.GradientStops[0].Color != Color.FromArgb(255, 195, 193, 194)) { 
       highlightBrush.GradientStops[0].Color = Color.FromArgb(255, 195, 193, 194); 
       highlightBrush.GradientStops[1].Color = Color.FromArgb(255, 157, 157, 157); 
      } 
     } 

我得到以下錯誤:「無法設置屬性'#FFFFFFFF,0',因爲它處於只讀狀態。「

有沒有人知道我在做什麼錯,或者什麼是最好的辦法。

+0

我沒有看到你的XAML定義的 'BackgroundBrush' 。根據您如何設置「例外」選項,當FindResource找不到「BackgroundBrush」時導致的錯誤將被忽略。 – Stewbob

+0

對不起,它不包括在我原來的帖子。我已更新我的帖子,以包括XAML的BackgroundBrush – TriStar

回答

3

IsMouseOver觸發踢,Freeze()叫上你的LinearGradientBrush,這反過來又使得它不可修改(如指出Stewbob)。一個凍結的對象不能被解凍,所以如果你需要修改那個特定的資源,除了克隆它,將它從資源中刪除並重新添加它之外,別無他法。這也需要您使用的DynamicResource代替StaticResource

觸發

<Trigger Property="IsMouseOver" Value="true"> 
    <Setter TargetName="HeaderBorder" 
      Property="Background" 
      Value="{DynamicResource HighlightBackgroundBrush}"/> 
    <!-- Additional setters.. --> 
</Trigger> 

代碼..

var highlightBrush = FindResource("HighlightBackgroundBrush") as LinearGradientBrush; 
if (highlightBrush != null) 
{ 
    if (highlightBrush.GradientStops[0].Color != Color.FromArgb(255, 195, 193, 194)) 
    { 
     this.Resources.Remove("HighlightBackgroundBrush"); 
     highlightBrush = highlightBrush.Clone(); 
     highlightBrush.GradientStops[0].Color = Color.FromArgb(255, 195, 193, 194); 
     highlightBrush.GradientStops[1].Color = Color.FromArgb(255, 157, 157, 157); 
     this.Resources.Add("HighlightBackgroundBrush", highlightBrush); 
    } 
} 
2

您的GradientStopCollection已凍結。據MSDN,發生這種情況時,你需要創建一個克隆和修改:

Dim hb0 As LinearGradientBrush = FindResource("BackgroundBrush") 
    If hb0.GradientStops.IsFrozen Then 
    Dim gs As GradientStopCollection = hb0.GradientStops.Clone 
    gs(0).Color = Color.FromArgb(255, 195, 193, 194) 
    gs(1).Color = Color.FromArgb(255, 157, 157, 157) 
    End If 

    Dim hb As LinearGradientBrush = FindResource("HighlightBackgroundBrush") 
    If hb.GradientStops.IsFrozen Then 
    Dim gs As GradientStopCollection = hb.GradientStops.Clone 
    gs(0).Color = Color.FromArgb(255, 195, 193, 194) 
    gs(1).Color = Color.FromArgb(255, 157, 157, 157) 
    End If 
+0

這修改了克隆顏色,而不是實際的畫筆。我在應用程序中的高亮顏色提醒白色 – TriStar