当前位置: 首页 > article >正文

WPF利用Path自定义画头部导航条(TOP)样式

1;新建两个多值转换器,都有用处,用来动态确定PATH的X,Y州坐标的。

EndPointConverter 该转换器主要用来动态确定X轴,和Y轴。用于画线条的。

internal class EndPointConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        double X = (double)values[0];//第一个值我这为上一个点X轴,可根据自己需要修改
        double C= double.Parse(parameter.ToString());//参数传差值,当前点的X轴与上一个点的X轴相差多少,
        double Y = 0;//默认0
        if (values.Length == 2)
        {
            Point StartPoint = (Point)values[1];//线条开始坐标共用Y轴
            Y = StartPoint.Y;
        }
        return new Point(X-C, Y);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

PointConverter 主要用来动态确定X轴,和Y轴。但当前Y轴是上一个点的X轴。用于闭合图形填充颜色的Path类型。

public class PointConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        double X = (double)values[0];
        double C = double.Parse(parameter.ToString());//参数传差值
        double Y = 0;//默认0
        if (values.Length == 2)
        {
            Point StartPoint = (Point)values[1];//线条开始坐标共用X轴作为Y轴
            Y = StartPoint.X;//下一个点的Y轴变为上个点的X轴,主要用来画闭合Path的
        }
        return new Point(X - C, Y);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

2;在写XML之前请在XMAL页面引入两个多值转换器(可根据自己需求自定义值类型和参数类型):

 <Window ....自己定义的其他内容....

xmlns:converters="clr-namespace:你的转换器所在路径"

......自己定义的其他内容.......>

<Window.Resources>
    //也可放置在App.XMAL下全局使用
    <converters:EndPointConverter x:Key="EndPointConverter" />
    <converters:PointConverter x:Key="PointConverter" />
</Window.Resources>

闭合Path的XMAL页面使用如下:

<!--闭合Path-->
<Path StrokeThickness="1.5" Stroke="#04386C">
    <Path.Data>
        <PathGeometry>
            <PathFigure  StartPoint="0,30">
                <LineSegment x:Name="Point1" Point="30,50"></LineSegment>
                <LineSegment x:Name="Point2">
                    <LineSegment.Point>
                        <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="30">
                            <Binding ElementName="ColorZone" Path="ActualWidth"/>
                            <Binding ElementName="Point1" Path="Point"/>
                        </MultiBinding>
                    </LineSegment.Point>
                </LineSegment>
                <LineSegment x:Name="Point3">
                    <LineSegment.Point>
                        <MultiBinding Converter="{StaticResource PointConverter}" ConverterParameter="0">
                            <Binding ElementName="ColorZone" Path="ActualWidth"/>
                            <Binding ElementName="Point1" Path="Point"/>
                        </MultiBinding>
                    </LineSegment.Point>
                </LineSegment>
                <LineSegment x:Name="Point4" >
                    <LineSegment.Point>
                        <MultiBinding Converter="{StaticResource PointConverter}" ConverterParameter="0">
                            <Binding ElementName="ColorZone" Path="ActualWidth"/>
                        </MultiBinding>
                    </LineSegment.Point>
                </LineSegment>
                <LineSegment Point="0,0"></LineSegment>
                <LineSegment Point="0,30"></LineSegment>
            </PathFigure>
        </PathGeometry>
    </Path.Data>
    <Path.Fill>
<!--渐变色设置-->
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Color="#07164C" Offset="0.4"></GradientStop>
            <GradientStop Color="#07205A" Offset="0.8"></GradientStop>
            <GradientStop Color="#07205B" Offset="1"></GradientStop>
        </LinearGradientBrush>
    </Path.Fill>
</Path>

效果:

线条Path的使用如下:

<Path StrokeThickness="3" Stroke="White">
        <Path.Data>
            <GeometryGroup>
                <LineGeometry x:Name="Point1" StartPoint="0 30" EndPoint="50 45" />
                <LineGeometry x:Name="Point2" StartPoint="50,45">
                    <LineGeometry.EndPoint>
                        <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="50">
                            <Binding ElementName="ColorZone" Path="ActualWidth"/>
                            <Binding Path="StartPoint" RelativeSource="{RelativeSource Mode=self}"/>
                        </MultiBinding>
                    </LineGeometry.EndPoint>
                </LineGeometry>
                <LineGeometry x:Name="Point3">
                    <LineGeometry.StartPoint>
                        <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="50">
                            <Binding ElementName="ColorZone" Path="ActualWidth"/>
                            <Binding ElementName="Point2" Path="StartPoint" />
                        </MultiBinding>
                    </LineGeometry.StartPoint>
                    <LineGeometry.EndPoint>
                        <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="0">
                            <Binding ElementName="ColorZone" Path="ActualWidth"/>
                            <Binding ElementName="Point1" Path="StartPoint" />
                        </MultiBinding>
                    </LineGeometry.EndPoint>
                </LineGeometry>
                <LineGeometry x:Name="Point4">
                    <LineGeometry.StartPoint>
                        <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="0">
                            <Binding ElementName="ColorZone" Path="ActualWidth"/>
                            <Binding ElementName="Point3" Path="EndPoint"/>
                        </MultiBinding>
                    </LineGeometry.StartPoint>
                    <LineGeometry.EndPoint>
                        <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="0">
                            <Binding ElementName="ColorZone" Path="ActualWidth"/>
                        </MultiBinding>
                    </LineGeometry.EndPoint>
                </LineGeometry>
                <LineGeometry x:Name="Point5" EndPoint="0 0">
                    <LineGeometry.StartPoint>
                        <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="0">
                            <Binding ElementName="ColorZone" Path="ActualWidth"/>
                        </MultiBinding>
                    </LineGeometry.StartPoint>
                </LineGeometry>
                <LineGeometry x:Name="Point6" StartPoint="0 0" EndPoint="0 30"/>
            </GeometryGroup>
        </Path.Data>
    </Path>

效果(白色才是效果色):

两种图形中的"ColorZone"是Path的父控件,materialDesign:ColorZone是WPF的UI框架materialDesign下的控件,可替换为你自己的控件作为父控件只是注意父控件名称修改下即可。

整体如下:

 <DockPanel>
     <!--头部-->
     <materialDesign:ColorZone Padding="0" Height="50"
                              
                       ClipToBounds="False" CornerRadius="5 5 0 0"
                         DockPanel.Dock="Top" x:Name="ColorZone">
         <StackPanel>
             <!--<Path StrokeThickness="1.5" Stroke="#04386C">
                 <Path.Data>
                     <PathGeometry>
                         <PathFigure  StartPoint="0,30">
                             <LineSegment x:Name="Point1" Point="30,50"></LineSegment>
                             <LineSegment x:Name="Point2">
                                 <LineSegment.Point>
                                     <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="30">
                                         <Binding ElementName="ColorZone" Path="ActualWidth"/>
                                         <Binding ElementName="Point1" Path="Point"/>
                                     </MultiBinding>
                                 </LineSegment.Point>
                             </LineSegment>
                             <LineSegment x:Name="Point3">
                                 <LineSegment.Point>
                                     <MultiBinding Converter="{StaticResource PointConverter}" ConverterParameter="0">
                                         <Binding ElementName="ColorZone" Path="ActualWidth"/>
                                         <Binding ElementName="Point1" Path="Point"/>
                                     </MultiBinding>
                                 </LineSegment.Point>
                             </LineSegment>
                             <LineSegment x:Name="Point4" >
                                 <LineSegment.Point>
                                     <MultiBinding Converter="{StaticResource PointConverter}" ConverterParameter="0">
                                         <Binding ElementName="ColorZone" Path="ActualWidth"/>
                                     </MultiBinding>
                                 </LineSegment.Point>
                             </LineSegment>
                             <LineSegment Point="0,0"></LineSegment>
                             <LineSegment Point="0,30"></LineSegment>
                         </PathFigure>
                     </PathGeometry>
                 </Path.Data>
                 <Path.Fill>
                     <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                         <GradientStop Color="#07164C" Offset="0.4"></GradientStop>
                         <GradientStop Color="#07205A" Offset="0.8"></GradientStop>
                         <GradientStop Color="#07205B" Offset="1"></GradientStop>
                     </LinearGradientBrush>
                 </Path.Fill>
             </Path>-->
             
         <Path StrokeThickness="3" Stroke="White">
                 <Path.Data>
                     <GeometryGroup>
                         <LineGeometry x:Name="Point1" StartPoint="0 30" EndPoint="50 45" />
                         <LineGeometry x:Name="Point2" StartPoint="50,45">
                             <LineGeometry.EndPoint>
                                 <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="50">
                                     <Binding ElementName="ColorZone" Path="ActualWidth"/>
                                     <Binding Path="StartPoint" RelativeSource="{RelativeSource Mode=self}"/>
                                 </MultiBinding>
                             </LineGeometry.EndPoint>
                         </LineGeometry>
                         <LineGeometry x:Name="Point3">
                             <LineGeometry.StartPoint>
                                 <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="50">
                                     <Binding ElementName="ColorZone" Path="ActualWidth"/>
                                     <Binding ElementName="Point2" Path="StartPoint" />
                                 </MultiBinding>
                             </LineGeometry.StartPoint>
                             <LineGeometry.EndPoint>
                                 <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="0">
                                     <Binding ElementName="ColorZone" Path="ActualWidth"/>
                                     <Binding ElementName="Point1" Path="StartPoint" />
                                 </MultiBinding>
                             </LineGeometry.EndPoint>
                         </LineGeometry>
                         <LineGeometry x:Name="Point4">
                             <LineGeometry.StartPoint>
                                 <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="0">
                                     <Binding ElementName="ColorZone" Path="ActualWidth"/>
                                     <Binding ElementName="Point3" Path="EndPoint"/>
                                 </MultiBinding>
                             </LineGeometry.StartPoint>
                             <LineGeometry.EndPoint>
                                 <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="0">
                                     <Binding ElementName="ColorZone" Path="ActualWidth"/>
                                 </MultiBinding>
                             </LineGeometry.EndPoint>
                         </LineGeometry>
                         <LineGeometry x:Name="Point5" EndPoint="0 0">
                             <LineGeometry.StartPoint>
                                 <MultiBinding Converter="{StaticResource EndPointConverter}" ConverterParameter="0">
                                     <Binding ElementName="ColorZone" Path="ActualWidth"/>
                                 </MultiBinding>
                             </LineGeometry.StartPoint>
                         </LineGeometry>
                         <LineGeometry x:Name="Point6" StartPoint="0 0" EndPoint="0 30"/>
                     </GeometryGroup>
                 </Path.Data>
             </Path>
         </StackPanel>
        
        
     </materialDesign:ColorZone>
     <!--底部-->
    
     <!--中部 只能放于最后,利用DockPanel的LastChildFill特性填充中间空间-->
    
 </DockPanel>

END.......虽然最终未采用该方式,但值得记录一下。


http://www.kler.cn/a/306602.html

相关文章:

  • HBase 安装与基本操作指南
  • #include<string>和#include<string.h>有什么区别
  • 【云计算解决方案面试整理】1-2云计算基础概念及云计算技术原理
  • JVM详解:JVM的系统架构
  • AMD CPU下pytorch 多GPU运行卡死和死锁解决
  • 网络远程操控
  • C# 网口通信(通过Sockets类)
  • Vue 生命周期与 TypeScript:深入理解组件生命周期
  • 5-【JavaWeb】JUnit 单元测试及JUL 日志系统
  • uniapp使用高德地图设置marker标记点,后续根据接口数据改变某个marker标记点,动态更新
  • CentOS 7上安装Docker
  • 电商数据采集分析全流程分享||电商API数据接口
  • wsl2桥接网络 ubuntu到弃坑到又跳坑
  • Flutter Spacer引发的The ParentDataWidget Expanded(flex: 1) 惨案
  • Spring Boot 注解探秘:JSON 处理的魔法世界
  • JAVA基础:线程优先级和精灵线程
  • 股指期货的详细玩法功能与应用解析
  • 详解 Pandas 的累计统计函数
  • Java后端开发(十七)-- Java中对Object、Collection集合、Map集合、数组进行判空
  • 数据结构:时间复杂度与空间复杂度
  • 【代码随想录训练营第42期 Day57打卡 - 图论Part7 - Prim算法与Kruskal算法
  • 后端开发刷题 | 数字字符串转化成IP地址
  • 状态机按键消抖(学习笔记)
  • Flink有界流实现(1)
  • 【Python】谷歌浏览器总是自动更新,使用selenium跟chromedriver版本不匹配怎么办?
  • 01,大数据总结,zookeeper