我有下面的模板設置更改的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',因爲它處於只讀狀態。「
有沒有人知道我在做什麼錯,或者什麼是最好的辦法。
我沒有看到你的XAML定義的 'BackgroundBrush' 。根據您如何設置「例外」選項,當FindResource找不到「BackgroundBrush」時導致的錯誤將被忽略。 – Stewbob
對不起,它不包括在我原來的帖子。我已更新我的帖子,以包括XAML的BackgroundBrush – TriStar