超炫酷的WPF实现Loading控件效果

Win8系统的Loading效果还是很不错的,网上也有人用CSS3等技术实现,研究了一下,并打算用WPF自定义一个Loading控件实现类似的效果,并可以让用户对Loading的颗粒(Particle)背景颜色进行自定义,话不多说,直接上代码:

1、用VS2012新建一个WPF的用户控件库项目WpfControlLibraryDemo,VS自动生成如下结构:

2、删除UserControl1.xaml,并新建一个Loading的CustomControl(不是UserControl),如下图所示:

3、如果报错找不到Loading类型,请编译,下面在Generic.xaml主题文件中对Loading的样式和内容进行定义(注意添加

xmlns:system = "clr-namespace:System;assembly=mscorlib"),代码如下:
<ResourceDictionary
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:system = "clr-namespace:System;assembly=mscorlib"
 xmlns:local="clr-namespace:WpfControlLibraryDemo">

 <Style TargetType="{x:Type local:Loading}">
 <Setter Property="Template">
  <Setter.Value>
  <ControlTemplate TargetType="{x:Type local:Loading}">
   <Border Background="{TemplateBinding Background}"
    BorderBrush="{TemplateBinding BorderBrush}"
    BorderThickness="{TemplateBinding BorderThickness}">
   <Grid Width = "50" Height = "50">
    <Grid.Resources>
    <!-- Value Converters -->

    <!-- Particle Styling ,must to has RelativeSource -->
    <SolidColorBrush x:Key = "ParticleColor" Color = "{Binding Path=FillColor,RelativeSource={RelativeSource TemplatedParent}}" />
    <SolidColorBrush x:Key = "ParticleBackgroundColor" Color = "Transparent"/>
    <system:Double x:Key = "ParticleOpacity">1</system:Double>
    <system:Double x:Key = "ParticleRadius">5</system:Double>

    <system:Double x:Key = "StartingPointX">0</system:Double>
    <system:Double x:Key = "StartingPointY">-20</system:Double>

    <system:Double x:Key = "RotationPointX">0.5</system:Double>
    <system:Double x:Key = "RotationPointY">0.5</system:Double>

    <!-- StoryBoard -->
    <system:TimeSpan x:Key = "StoryBoardBeginTimeP0">00:00:00.000</system:TimeSpan>
    <system:TimeSpan x:Key = "StoryBoardBeginTimeP1">00:00:00.100</system:TimeSpan>
    <system:TimeSpan x:Key = "StoryBoardBeginTimeP2">00:00:00.200</system:TimeSpan>
    <system:TimeSpan x:Key = "StoryBoardBeginTimeP3">00:00:00.300</system:TimeSpan>
    <system:TimeSpan x:Key = "StoryBoardBeginTimeP4">00:00:00.400</system:TimeSpan>
    <Duration x:Key = "StoryBoardDuration">00:00:01.800</Duration>

    <!-- Particle Origin Angles -->
    <system:Double x:Key = "ParticleOriginAngleP0">0</system:Double>
    <system:Double x:Key = "ParticleOriginAngleP1">-10</system:Double>
    <system:Double x:Key = "ParticleOriginAngleP2">-20</system:Double>
    <system:Double x:Key = "ParticleOriginAngleP3">-30</system:Double>
    <system:Double x:Key = "ParticleOriginAngleP4">-40</system:Double>

    <!-- Particle Position & Timing 1 -->
    <system:Double x:Key = "ParticleBeginAngle1">0</system:Double>
    <system:Double x:Key = "ParticleEndAngle1">90</system:Double>
    <system:TimeSpan x:Key = "ParticleBeginTime1">00:00:00.000</system:TimeSpan>
    <Duration x:Key = "ParticleDuration1">00:00:00.750</Duration>

    <!-- Particle Position & Timing 2 -->
    <system:Double x:Key = "ParticleBeginAngle2">90</system:Double>
    <system:Double x:Key = "ParticleEndAngle2">270</system:Double>
    <system:TimeSpan x:Key = "ParticleBeginTime2">00:00:00.751</system:TimeSpan>
    <Duration x:Key = "ParticleDuration2">00:00:00.300</Duration>

    <!-- Particle Position & Timing 3 -->
    <system:Double x:Key = "ParticleBeginAngle3">270</system:Double>
    <system:Double x:Key = "ParticleEndAngle3">360</system:Double>
    <system:TimeSpan x:Key = "ParticleBeginTime3">00:00:01.052</system:TimeSpan>
    <Duration x:Key = "ParticleDuration3">00:00:00.750</Duration>

    <Style x:Key = "EllipseStyle" TargetType = "Ellipse">
     <Setter Property = "Width" Value = "{StaticResource ParticleRadius}"/>
     <Setter Property = "Height" Value = "{StaticResource ParticleRadius}"/>
     <Setter Property = "Fill" Value = "{StaticResource ParticleColor}"/>
     <Setter Property = "RenderTransformOrigin" Value = "0.5, 0.5"/>
     <Setter Property = "Opacity" Value = "{StaticResource ParticleOpacity}"/>
    </Style>
    </Grid.Resources>
    <Canvas Width = "1" Height = "1" Margin="0,0,0,0">
    <Canvas.Triggers>
     <EventTrigger RoutedEvent = "Canvas.Loaded">
     <EventTrigger.Actions>
      <BeginStoryboard>
      <Storyboard

    BeginTime = "{StaticResource StoryBoardBeginTimeP0}"
    Duration = "{StaticResource StoryBoardDuration}"
    RepeatBehavior = "Forever">
       <DoubleAnimation
    Storyboard.TargetName = "p0"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle1}"
    To = "{StaticResource ParticleEndAngle1}"
    BeginTime = "{StaticResource ParticleBeginTime1}"
    Duration = "{StaticResource ParticleDuration1}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p0"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle2}"
    To = "{StaticResource ParticleEndAngle2}"
    BeginTime = "{StaticResource ParticleBeginTime2}"
    Duration = "{StaticResource ParticleDuration2}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p0"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle3}"
    To = "{StaticResource ParticleEndAngle3}"
    BeginTime = "{StaticResource ParticleBeginTime3}"
    Duration = "{StaticResource ParticleDuration3}"/>
      </Storyboard>
      </BeginStoryboard>
      <BeginStoryboard>
      <Storyboard

    BeginTime = "{StaticResource StoryBoardBeginTimeP1}"
    Duration = "{StaticResource StoryBoardDuration}"
    RepeatBehavior = "Forever">

       <DoubleAnimation
    Storyboard.TargetName = "p1"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle1}"
    To = "{StaticResource ParticleEndAngle1}"
    BeginTime = "{StaticResource ParticleBeginTime1}"
    Duration = "{StaticResource ParticleDuration1}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p1"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle2}"
    To = "{StaticResource ParticleEndAngle2}"
    BeginTime = "{StaticResource ParticleBeginTime2}"
    Duration = "{StaticResource ParticleDuration2}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p1"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle3}"
    To = "{StaticResource ParticleEndAngle3}"
    BeginTime = "{StaticResource ParticleBeginTime3}"
    Duration = "{StaticResource ParticleDuration3}"/>
      </Storyboard>
      </BeginStoryboard>
      <BeginStoryboard>
      <Storyboard

    BeginTime = "{StaticResource StoryBoardBeginTimeP2}"
    Duration = "{StaticResource StoryBoardDuration}"
    RepeatBehavior = "Forever">

       <DoubleAnimation
    Storyboard.TargetName = "p2"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle1}"
    To = "{StaticResource ParticleEndAngle1}"
    BeginTime = "{StaticResource ParticleBeginTime1}"
    Duration = "{StaticResource ParticleDuration1}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p2"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle2}"
    To = "{StaticResource ParticleEndAngle2}"
    BeginTime = "{StaticResource ParticleBeginTime2}"
    Duration = "{StaticResource ParticleDuration2}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p2"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle3}"
    To = "{StaticResource ParticleEndAngle3}"
    BeginTime = "{StaticResource ParticleBeginTime3}"
    Duration = "{StaticResource ParticleDuration3}"/>
      </Storyboard>
      </BeginStoryboard>

      <BeginStoryboard>
      <Storyboard

    BeginTime = "{StaticResource StoryBoardBeginTimeP3}"
    Duration = "{StaticResource StoryBoardDuration}"
    RepeatBehavior = "Forever">

       <DoubleAnimation
    Storyboard.TargetName = "p3"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle1}"
    To = "{StaticResource ParticleEndAngle1}"
    BeginTime = "{StaticResource ParticleBeginTime1}"
    Duration = "{StaticResource ParticleDuration1}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p3"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle2}"
    To = "{StaticResource ParticleEndAngle2}"
    BeginTime = "{StaticResource ParticleBeginTime2}"
    Duration = "{StaticResource ParticleDuration2}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p3"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle3}"
    To = "{StaticResource ParticleEndAngle3}"
    BeginTime = "{StaticResource ParticleBeginTime3}"
    Duration = "{StaticResource ParticleDuration3}"/>
      </Storyboard>
      </BeginStoryboard>

      <BeginStoryboard>
      <Storyboard

    BeginTime = "{StaticResource StoryBoardBeginTimeP4}"
    Duration = "{StaticResource StoryBoardDuration}"
    RepeatBehavior = "Forever">

       <DoubleAnimation
    Storyboard.TargetName = "p4"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle1}"
    To = "{StaticResource ParticleEndAngle1}"
    BeginTime = "{StaticResource ParticleBeginTime1}"
    Duration = "{StaticResource ParticleDuration1}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p4"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle2}"
    To = "{StaticResource ParticleEndAngle2}"
    BeginTime = "{StaticResource ParticleBeginTime2}"
    Duration = "{StaticResource ParticleDuration2}"/>
       <DoubleAnimation
    Storyboard.TargetName = "p4"
    Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
    From = "{StaticResource ParticleBeginAngle3}"
    To = "{StaticResource ParticleEndAngle3}"
    BeginTime = "{StaticResource ParticleBeginTime3}"
    Duration = "{StaticResource ParticleDuration3}"/>
      </Storyboard>
      </BeginStoryboard>
     </EventTrigger.Actions>
     </EventTrigger>
    </Canvas.Triggers>
    <Border
  x:Name = "p0"
  Background = "{StaticResource ParticleBackgroundColor}"
  Opacity = "{StaticResource ParticleOpacity}">
     <Border.RenderTransform>
     <RotateTransform/>
     </Border.RenderTransform>
     <Border.RenderTransformOrigin>
     <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
     </Border.RenderTransformOrigin>
     <Ellipse Style = "{StaticResource EllipseStyle}">
     <Ellipse.RenderTransform>
      <TransformGroup>
      <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
      <RotateTransform Angle = "{StaticResource ParticleOriginAngleP0}"/>
      </TransformGroup>
     </Ellipse.RenderTransform>
     </Ellipse>
    </Border>
    <Border
  x:Name = "p1"
  Background = "{StaticResource ParticleBackgroundColor}"
  Opacity = "{StaticResource ParticleOpacity}">
     <Border.RenderTransform>
     <RotateTransform/>
     </Border.RenderTransform>
     <Border.RenderTransformOrigin>
     <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
     </Border.RenderTransformOrigin>
     <Ellipse Style = "{StaticResource EllipseStyle}">
     <Ellipse.RenderTransform>
      <TransformGroup>
      <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
      <RotateTransform Angle = "{StaticResource ParticleOriginAngleP1}"/>
      </TransformGroup>
     </Ellipse.RenderTransform>
     </Ellipse>
    </Border>
    <Border
  x:Name = "p2"
  Background = "{StaticResource ParticleBackgroundColor}"
  Opacity = "{StaticResource ParticleOpacity}">
     <Border.RenderTransform>
     <RotateTransform/>
     </Border.RenderTransform>
     <Border.RenderTransformOrigin>
     <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
     </Border.RenderTransformOrigin>
     <Ellipse Style = "{StaticResource EllipseStyle}">
     <Ellipse.RenderTransform>
      <TransformGroup>
      <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
      <RotateTransform Angle = "{StaticResource ParticleOriginAngleP2}"/>
      </TransformGroup>
     </Ellipse.RenderTransform>
     </Ellipse>
    </Border>
    <Border
  x:Name = "p3"
  Background = "{StaticResource ParticleBackgroundColor}"
  Opacity = "{StaticResource ParticleOpacity}">
     <Border.RenderTransform>
     <RotateTransform/>
     </Border.RenderTransform>
     <Border.RenderTransformOrigin>
     <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
     </Border.RenderTransformOrigin>
     <Ellipse Style = "{StaticResource EllipseStyle}">
     <Ellipse.RenderTransform>
      <TransformGroup>
      <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
      <RotateTransform Angle = "{StaticResource ParticleOriginAngleP3}"/>
      </TransformGroup>
     </Ellipse.RenderTransform>
     </Ellipse>
    </Border>
    <Border
  x:Name = "p4"
  Background = "{StaticResource ParticleBackgroundColor}"
  Opacity = "{StaticResource ParticleOpacity}">
     <Border.RenderTransform>
     <RotateTransform/>
     </Border.RenderTransform>
     <Border.RenderTransformOrigin>
     <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
     </Border.RenderTransformOrigin>
     <Ellipse Style = "{StaticResource EllipseStyle}">
     <Ellipse.RenderTransform>
      <TransformGroup>
      <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
      <RotateTransform Angle = "{StaticResource ParticleOriginAngleP4}"/>
      </TransformGroup>
     </Ellipse.RenderTransform>
     </Ellipse>
    </Border>
    </Canvas>
   </Grid>

   </Border>
  </ControlTemplate>
  </Setter.Value>
 </Setter>
 </Style>

</ResourceDictionary>

在构建中发现,一开始在设定绑定时,写成<SolidColorBrush x:Key = "ParticleColor" Color = "{Binding Path=FillColor}" />一直都无法绑定成功,后来查了资料,改成<SolidColorBrush x:Key = "ParticleColor" Color = "{Binding Path=FillColor,RelativeSource={RelativeSource TemplatedParent}}" /> 后成功。

4、编辑Loading.cs文件,对自定义属性FillColor和逻辑进行编码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfControlLibraryDemo
{
 using System.ComponentModel;
 /// <summary>
 /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
 ///
 /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
 /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
 /// 元素中:
 ///
 /// xmlns:MyNamespace="clr-namespace:WpfControlLibraryDemo"
 ///
 ///
 /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
 /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
 /// 元素中:
 ///
 /// xmlns:MyNamespace="clr-namespace:WpfControlLibraryDemo;assembly=WpfControlLibraryDemo"
 ///
 /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
 /// 并重新生成以避免编译错误:
 ///
 /// 在解决方案资源管理器中右击目标项目,然后依次单击
 /// “添加引用”->“项目”->[浏览查找并选择此项目]
 ///
 ///
 /// 步骤 2)
 /// 继续操作并在 XAML 文件中使用控件。
 ///
 /// <MyNamespace:Loading/>
 ///
 /// </summary>
 public class Loading : Control
 {
 static Loading()
 {
  //重载默认样式
  DefaultStyleKeyProperty.OverrideMetadata(typeof(Loading), new FrameworkPropertyMetadata(typeof(Loading)));
  //DependencyProperty 注册 FillColor
  FillColorProperty = DependencyProperty.Register("FillColor",
  typeof(Color),
  typeof(Loading),
  new UIPropertyMetadata(Colors.DarkBlue,
  new PropertyChangedCallback(OnUriChanged))
  );
  //Colors.DarkBlue为控件初始化默认值

 }
 //属性变更回调函数
 private static void OnUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
  //Border b = (Border)d;
  //MessageBox.Show(e.NewValue.ToString());

 }
 #region 自定义Fields
 // DependencyProperty属性定义 FillColorProperty=FillColor+Property组成
 public static readonly DependencyProperty FillColorProperty;
 #endregion
 //VS设计器属性支持
 [Description("背景色"), Category("个性配置"), DefaultValue("#FF668899")]
 public Color FillColor
 {
  //GetValue,SetValue为固定写法,此处一般不建议处理其他逻辑
  get { return (Color)GetValue(FillColorProperty); }
  set { SetValue(FillColorProperty, value); }
 }
 }
}

5、编译,如果无误后,可以添加WPF应用程序WpfAppLoadingTest进行测试(添加项目引用)。

打开MainWindow.xaml,将Loading控件拖放到设计界面上,如下图所示:

6、控件颜色修改,选中控件,在属性栏中进行配置即可:

7.总结

可以看到WPF自定义控件还是比较容易的,但是难点在于UI的设计,如果需要做的美观,需要美工的参与,而且需要转换成XAML。

以上就是WPF实现炫酷Loading控件的全部内容,希望对大家的学习有所帮助。

(0)

相关推荐

  • WPF中在摄像头视频上叠加控件的解决方案

    说道WPF想必有很多朋友跟小编一样不知道wpf是什么,今天小编就给大家简单普及下基本概念. WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,属于.NET Framework 3.0的一部分.它提供了统一的编程模型.语言和框架,真正做到了分离界面设计人员与开发人员的工作:同时它提供了全新的多媒体交互用户图形界面. 一.视频呈现 前段时间,在一个wpf的项目中需要实时显示ip摄像头,对此的解决方案想必大家都应该知道很多.在win

  • WPF中不规则窗体与WindowsFormsHost控件兼容问题的解决方法

    本文实例讲述了WPF中不规则窗体与WindowsFormsHost控件兼容问题的解决方法.分享给大家供大家参考.具体方法如下: 这里首先说明一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的很多解决方案不能满足所有的情况,是有特定条件的,比如有一篇<WPF中不规则窗体与WebBrowser控件的兼容问题解决办法>(感兴趣的朋友可以自己百度一下这篇文章).该网友的解决办法也是别出心裁的,为什么这样说呢,他的webBrowser控件的是单独放在一个Form中

  • WPF图形解锁控件ScreenUnLock使用详解

    ScreenUnLock 与智能手机上的图案解锁功能一样.通过绘制图形达到解锁或记忆图形的目的. 本人突发奇想,把手机上的图形解锁功能移植到WPF中.也应用到了公司的项目中. 在创建ScreenUnLock之前,先来分析一下图形解锁的实现思路. 1.创建九宫格原点(或更多格子),每个点定义一个坐标值 2.提供图形解锁相关扩展属性和事件,方便调用者定义.比如:点和线的颜色(Color),操作模式(Check|Remember),验证正确的颜色(RightColor), 验证失败的颜色(ErrorC

  • 在WPF中动态加载XAML中的控件实例代码

    本文实例讲述了在WPF中动态加载XAML中的控件的方法.分享给大家供大家参考,具体如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using S

  • C# WPF ListView控件的实例详解

    C# WPF ListView控件的实例详解 C#的WPF作为现在微软主流的桌面程序开发平台,相比过去的MFC时代,有了非常多的不同.本人刚从MFC平台转过来,以为可以轻松上手,哪知碰到了很多问题,十分不解.不得不乖乖回去看了本书,再继续回到边左边边学的路上.在这边也推荐<深入浅出WPF>这本书,拿来上手还是极好的. 由于WPF以数据驱动UI的设计理念,很多控件用起来都与之前平台的相差很多,ListView控件算是有代表性的,这是进化的成果.关于该控件的应该,很多参考了这篇博文,如觉本人记述不

  • WPF的ListView控件自定义布局用法实例

    本文实例讲述了WPF的ListView控件自定义布局用法.分享给大家供大家参考,具体如下: 概要: 以源码的形式贴出,免得忘记后,再到网上查资料.在VS2008+SP1环境下调试通过 引用的GrayscaleEffect模块,可根据参考资料<Grayscale Effect...>中的位置下载. 正文: 如何布局是在App.xaml中定义源码如下 <Application x:Class="CWebsSynAssistant.App" xmlns="http

  • WPF自定义选择年月控件详解

    本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: <UserControl x:Class="SunCreate.CombatPlatform.Client.DateMonthPicker" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.micr

  • WPF实现带全选复选框的列表控件

    本文将说明如何创建一个带全选复选框的列表控件.其效果如下图: 这个控件是由一个复选框(CheckBox)与一个 ListView 组合而成.它的操作逻辑: 当选中"全选"时,列表中所有的项目都会被选中:反之,取消选中"全选"时,所有项都会被取消勾选. 在列表中选中部分数据项目时,"全选"框会呈现不确定状态(Indetermine). 由此看出,"全选"复选框与列表项中的复选框达到了双向控制的效果. 其设计思路:首先,创建自定义

  • WPF实现ScrollViewer滚动到指定控件处

    在前端 UI 开发中,有时,我们会遇到这样的需求:在一个 ScrollViewer 中有很多内容,而我们需要实现在执行某个操作后能够定位到其中指定的控件处:这很像在 HTML 页面中点击一个链接后定位到当前网页上的某个 anchor. 要实现它,首先我们需要看 ScrollViewer 为我们提供的 API,其中并没有类似于 ScrollToControl 这样的方法:在它的几个以 ScrollTo 开头的方法中,最合适的就是 ScrollToVerticalOffset 这个方法了,这个方法接

  • WPF中引入WindowsForms控件的方法

    本文实例讲述了WPF中引入WindowsForms控件的方法.分享给大家供大家参考,具体如下: 环境: [1]WindowsXP with SP3 [2]VS2008 with SP1 正文: Step1:在现有工程中引入Windows Forms 鼠标右键[References]->选择[Add Reference]->[.NET]标签页 加入[WindowsFormsIntegration]和[System.Windows.Forms]两项 Step2:在XAML文件里加入 [S2-1]加

随机推荐