【问题标题】:WPF geometry (Path) partly filled shapesWPF 几何(路径)部分填充的形状
【发布时间】:2011-09-18 12:33:15
【问题描述】:

这个想法是获得评级控制,它的值可能像 0.3 并将其绘制为部分填充的形状。
我使用的方法在 CodeProject 文章中进行了描述。有Path,添加蒙版(矩形),添加轮廓。
用于掩码和固定宽度路径的原始代码 Margin
问题是使用矩形作为蒙版会重新绘制背景,这是渐变的,所以我不能为蒙版设置相同的背景值。 我改变了颜色只是为了让它更清楚。

是否可以模拟部分填充的路径?

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

    <Grid x:Name="gdStar" Width="Auto" Height="Auto">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.5*"/>
            <ColumnDefinition Width="0.5*"/>
        </Grid.ColumnDefinitions>

        <Path 
            Grid.ColumnSpan="2"
        Fill="Red" Stretch="Fill" Stroke="Blue" StrokeThickness="1"  Margin="-0.5,-0.5,-0.5,-0.019" Data="M63.50031,-0.50054431 L47.500523,55.499079 -0.49883747,55.499079 39.50063,95.498578 23.500843,159.49799 63.833676,128.4989 103.7623,160.51698 87.095797,95.850405 127.49946,55.499079 79.500097,55.499079 z"/>

        <Rectangle Grid.Column="1"
        Fill="Yellow"/>

        <Path 
            Grid.ColumnSpan="2"
            Fill="Transparent" Stretch="Fill" Stroke="Red" StrokeThickness="1" Margin="-0.5,-0.5,-0.5,-0.019" Data="M63.50031,-0.50054431 L47.500523,55.499079 -0.49883747,55.499079 39.50063,95.498578 23.500843,159.49799 63.833676,128.4989 103.7623,160.51698 87.095797,95.850405 127.49946,55.499079 79.500097,55.499079 z"/>

    </Grid>


</Page>

编辑
这可能是错误的方法。还能用什么? OpacityBrush/剪辑?

【问题讨论】:

    标签: wpf path geometry


    【解决方案1】:

    您是否正在寻找这样的东西:

    <Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">   
        <Grid x:Name="gdStar" Width="Auto" Height="Auto">
            <Path 
            Fill="Red" Stretch="Fill" Stroke="Blue" StrokeThickness="1"  Margin="-0.5,-0.5,-0.5,-0.019" Data="M63.50031,-0.50054431 L47.500523,55.499079 -0.49883747,55.499079 39.50063,95.498578 23.500843,159.49799 63.833676,128.4989 103.7623,160.51698 87.095797,95.850405 127.49946,55.499079 79.500097,55.499079 z">
                <Path.OpacityMask>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                        <GradientStop Color="Transparent" Offset="0.3"/>
                        <GradientStop Color="White" Offset="0.3"/>
                    </LinearGradientBrush>
                </Path.OpacityMask>
            </Path>
            <Path 
                Fill="Transparent" Stretch="Fill" Stroke="Red" StrokeThickness="1" Margin="-0.5,-0.5,-0.5,-0.019" Data="M63.50031,-0.50054431 L47.500523,55.499079 -0.49883747,55.499079 39.50063,95.498578 23.500843,159.49799 63.833676,128.4989 103.7623,160.51698 87.095797,95.850405 127.49946,55.499079 79.500097,55.499079 z"/> 
        </Grid>
    </Page>
    

    编辑这是一个通过在其后面添加一个白星来更改未填充区域背景的版本,我将之前的示例留在那里,因为它更清晰一些。

    <Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="yellow">   
        <Grid x:Name="gdStar" Width="Auto" Height="Auto">
            <Path 
            Fill="White" Stretch="Fill" Stroke="Blue" StrokeThickness="1"  Margin="-0.5,-0.5,-0.5,-0.019" Data="M63.50031,-0.50054431 L47.500523,55.499079 -0.49883747,55.499079 39.50063,95.498578 23.500843,159.49799 63.833676,128.4989 103.7623,160.51698 87.095797,95.850405 127.49946,55.499079 79.500097,55.499079 z"/>
            <Path 
            Fill="Red" Stretch="Fill" Stroke="Blue" StrokeThickness="1"  Margin="-0.5,-0.5,-0.5,-0.019" Data="M63.50031,-0.50054431 L47.500523,55.499079 -0.49883747,55.499079 39.50063,95.498578 23.500843,159.49799 63.833676,128.4989 103.7623,160.51698 87.095797,95.850405 127.49946,55.499079 79.500097,55.499079 z">
                <Path.OpacityMask>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                        <GradientStop Color="Transparent" Offset="0.3"/>
                        <GradientStop Color="White" Offset="0.3"/>
                    </LinearGradientBrush>
                </Path.OpacityMask>
            </Path>
            <Path 
                Fill="Transparent" Stretch="Fill" Stroke="Red" StrokeThickness="1" Margin="-0.5,-0.5,-0.5,-0.019" Data="M63.50031,-0.50054431 L47.500523,55.499079 -0.49883747,55.499079 39.50063,95.498578 23.500843,159.49799 63.833676,128.4989 103.7623,160.51698 87.095797,95.850405 127.49946,55.499079 79.500097,55.499079 z"/> 
        </Grid>
    </Page>
    

    【讨论】:

    • 快到了。如果父网格有一些背景 - 例如黄色,那么路径的“未填充”部分也将是黄色。可以白做吗?
    • 简单,看第二个例子
    猜你喜欢
    • 1970-01-01
    • 2018-03-01
    • 2019-07-06
    • 2023-03-22
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2020-06-19
    相关资源
    最近更新 更多