【问题标题】:VisualBrush color not changing when selected选择时VisualBrush颜色不会改变
【发布时间】:2014-10-17 07:54:34
【问题描述】:

我在 WPF 中创建了一个 VisualBrush,为角色添加波浪形下划线。

<VisualBrush x:Key="WavyBrush">
    <VisualBrush.Visual>
        <Path Data="M 0,2 L 2,0 4,2 6,0 8,2 10,0 12,2" Stroke="Black" />
    </VisualBrush.Visual>
</VisualBrush>

效果很好,除非我将结果放入 DataGrid 并选择行。如果未选择的行具有带有白色背景的黑色文本,并且选择该行会将文本变为白色(以及背景为蓝色或其他颜色),我的 Stroke="Black" 的 VisualBrush 将保持黑色,不会变为白色并且看起来令人困惑。

有没有办法让画笔表现得像文本颜色?

【问题讨论】:

  • 以这种方式使用VisualBrush 不是它的设计目的。如果你想把一个Path的数据变成一些Brush,你可以只使用DrawingBrush,那么你可以只使用Path的数据为PathGeometry
  • 评论完美地解决了我的问题。你想把它作为答案吗?
  • 很高兴知道您可以在改用DrawingBrush 时轻松解决此问题。我只是建议使用它,因为它更方便,但是使用 VisualBrush 应该不是问题(您仍然可以使用它使其工作)。我不太确定它与您的问题有多接近,我的意思是您几乎必须自己解决,所以最好不要添加答案,无论如何,谢谢。

标签: wpf


【解决方案1】:

您可以使用DataTrigger 更改选中行时画笔的颜色。该代码使用ListBox,但您可以轻松地将其修改为使用DataGrid

<Window.Resources>
    <VisualBrush x:Key="WavyBrush">
        <VisualBrush.Visual>
            <Path Data="M 0,2 L 2,0 4,2 6,0 8,2 10,0 12,2" Stroke="Black" />
        </VisualBrush.Visual>
    </VisualBrush>
    <VisualBrush x:Key="whiteWavyBrush">
        <VisualBrush.Visual>
            <Path Data="M 0,2 L 2,0 4,2 6,0 8,2 10,0 12,2" Stroke="White" />
        </VisualBrush.Visual>
    </VisualBrush>
</Window.Resources>
<Grid>
    <ListBox ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Height="50" Width="350">
                    <Border x:Name="border" Width="350" Height="10" VerticalAlignment="Bottom" Background="{StaticResource WavyBrush}">
                    </Border>
                </Grid>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                        <Setter TargetName="border" Property="Background" Value="{StaticResource whiteWavyBrush}" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

【讨论】:

  • that's fine, I guess the framework handles DrawingBrush differently, it auto reverse the drawing's color (to hightlight) when the drawing is selected;另一方面,VisualBrush 比较原始。
【解决方案2】:

如果其他人有兴趣,DrawingBrush 就像文本一样,在被选中时会改变颜色。

    <DrawingBrush x:Key="TextBoxWavyBrush">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <DrawingGroup.Children>
                    <GeometryDrawing Geometry="M 0,2 L 2,0 4,2 6,0 8,2 10,0 12,2">
                        <GeometryDrawing.Pen>
                            <Pen Thickness="1" StartLineCap="Round" EndLineCap="Round" LineJoin="Round" Brush="{Binding RelativeSource={RelativeSource AncestorType=TextBox, Mode=FindAncestor}, Path=Foreground}"/>
                        </GeometryDrawing.Pen>
                    </GeometryDrawing>
                </DrawingGroup.Children>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    相关资源
    最近更新 更多