我有以下模板设置
<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>
在DataContextChanged事件期间的代码隐藏中,我试图更改Header Column 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);
}
}
然而,当我尝试做一些类似的事情来调整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 :(得分: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);
}
}
答案 1 :(得分:2)
您的GradientStopCollection已冻结。根据{{3}},当发生这种情况时,您需要创建一个克隆并修改它:
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