三略 2024-09-27 17:12 采纳率: 57.1%
浏览 11

wpf的Grid 绑定渐变色无效

<Grid VerticalAlignment="Bottom" Margin="0">
                            <!--{TemplateBinding MusicBackground} {TemplateBinding MusicHeight} -->
                            <!--Background="{Binding MusicBackground1,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}"-->
                            <Grid   Height="{Binding MusicHeight,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}"  Margin="0" VerticalAlignment="Bottom">
                                <Grid.Background>
                                    <LinearGradientBrush StartPoint="0,1" EndPoint="0,1">
                                        <GradientStop Color="{Binding MusicBackground1,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}" Offset="0.1"/>
                                        <GradientStop Color="{Binding MusicBackground2,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}" Offset="0.5"/>
                                        <GradientStop Color="{Binding MusicBackground3,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}" Offset="1.0"/>
                                    </LinearGradientBrush>
                                </Grid.Background>
                                <Canvas Margin="-1,-6" >
                                    <Rectangle x:Name="PART_SelectionRange" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Visibility="Hidden" Width="4.0">
                                    </Rectangle>
                                </Canvas>
                            </Grid>
                        </Grid>
                        


但是如把颜色的值写死也是有效的
但是直接这样写成绑定也是有效的


```xml
<Grid VerticalAlignment="Bottom" Margin="0">
                            <!--{TemplateBinding MusicBackground} {TemplateBinding MusicHeight} -->
                            <!--Background="{Binding MusicBackground1,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}"-->
                            <Grid Background="{Binding MusicBackground1,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}"  Height="{Binding MusicHeight,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}"  Margin="0" VerticalAlignment="Bottom">
               
                                <Canvas Margin="-1,-6" >
                                    <Rectangle x:Name="PART_SelectionRange" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Visibility="Hidden" Width="4.0">
                                    </Rectangle>
                                </Canvas>
                            </Grid>
                        </Grid>


```

  • 写回答

3条回答 默认 最新

  • 趣享先生 优质创作者: 编程框架技术领域 2024-09-27 17:18
    关注

    在WPF中,使用Grid控件绑定渐变色是无效的,因为Grid控件本身不具备背景属性,无法直接设置渐变色。如果你想要实现背景渐变色效果,可以考虑将Grid控件替换为一个具有背景属性的控件,比如Border或者Panel,并在该控件上设置背景为渐变色。

    以下是一种可能的解决方案,你可以尝试将外层的Grid控件替换为Border控件,并在Border控件的Background属性中设置渐变色,代码示例如下:

    <Border VerticalAlignment="Bottom" Margin="0">
        <Border.Background>
            <LinearGradientBrush StartPoint="0,1" EndPoint="0,1">
                <GradientStop Color="{Binding MusicBackground1,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}" Offset="0.1"/>
                <GradientStop Color="{Binding MusicBackground2,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}" Offset="0.5"/>
                <GradientStop Color="{Binding MusicBackground3,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}" Offset="1.0"/>
            </LinearGradientBrush>
        </Border.Background>
        <Grid Height="{Binding MusicHeight,RelativeSource={RelativeSource AncestorType={x:Type local:MusicSlider}}}" Margin="0" VerticalAlignment="Bottom">
            <Canvas Margin="-1,-6">
                <Rectangle x:Name="PART_SelectionRange" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Visibility="Hidden" Width="4.0">
                </Rectangle>
            </Canvas>
        </Grid>
    </Border>
    

    通过将外层的Grid控件替换为Border控件,并在Border的Background属性中设置渐变色,可以实现背景渐变色效果。

    该回答引用于ChatGPT(答题不易,还望博友采纳!)

    评论

报告相同问题?

问题事件

  • 创建了问题 9月27日