WPF 加载中Loading,自定义控件

模仿Win10-加载中Loading:

loading

用Ellipse画一个圆点

 <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>

利用ContentControl的RenderTransform实现旋转

<ContentControl RenderTransformOrigin="0.5,0.5" x:Name="item1">
    <ContentControl.RenderTransform>
        <RotateTransform Angle="0"/>
    </ContentControl.RenderTransform>
    <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>
</ContentControl>

RenderTransformOrigin=“0.5,0.5” 是为了设置旋转中心。

总共画五个点,如下:

<Grid Width="100" Height="100" Margin="0 50 0 0">
    <ContentControl RenderTransformOrigin="0.5,0.5" x:Name="item1">
        <ContentControl.RenderTransform>
            <RotateTransform Angle="0"/>
        </ContentControl.RenderTransform>
        <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>
    </ContentControl>
    <ContentControl RenderTransformOrigin="0.5,0.5" x:Name="item2">
        <ContentControl.RenderTransform>
            <RotateTransform Angle="0"/>
        </ContentControl.RenderTransform>
        <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>
    </ContentControl>
    <ContentControl RenderTransformOrigin="0.5,0.5" x:Name="item3">
        <ContentControl.RenderTransform>
            <RotateTransform Angle="0"/>
        </ContentControl.RenderTransform>
        <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>
    </ContentControl>
    <ContentControl RenderTransformOrigin="0.5,0.5" x:Name="item4">
        <ContentControl.RenderTransform>
            <RotateTransform Angle="0"/>
        </ContentControl.RenderTransform>
        <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>
    </ContentControl>
</Grid>

旋转代码:

 <BeginStoryboard>
     <Storyboard>
         <DoubleAnimationUsingKeyFrames RepeatBehavior="Forever"
                                        Storyboard.TargetName="item1" Storyboard.TargetProperty="RenderTransform.Angle">
             <SplineDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
             <SplineDoubleKeyFrame Value="360" KeyTime="0:0:2" KeySpline="0.07,0.39,0.92,0.59"/>
             <SplineDoubleKeyFrame Value="720" KeyTime="0:0:4" KeySpline="0.07,0.39,0.92,0.59"/>
         </DoubleAnimationUsingKeyFrames>
         <DoubleAnimationUsingKeyFrames RepeatBehavior="Forever" BeginTime="0:0:0.2"
                                        Storyboard.TargetName="item2" Storyboard.TargetProperty="RenderTransform.Angle">
             <SplineDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
             <SplineDoubleKeyFrame Value="360" KeyTime="0:0:2" KeySpline="0.07,0.39,0.92,0.59"/>
             <SplineDoubleKeyFrame Value="720" KeyTime="0:0:4" KeySpline="0.07,0.39,0.92,0.59"/>
         </DoubleAnimationUsingKeyFrames>
         <DoubleAnimationUsingKeyFrames RepeatBehavior="Forever"  BeginTime="0:0:0.4"
                                        Storyboard.TargetName="item3" Storyboard.TargetProperty="RenderTransform.Angle">
             <SplineDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
             <SplineDoubleKeyFrame Value="360" KeyTime="0:0:2" KeySpline="0.07,0.39,0.92,0.59"/>
             <SplineDoubleKeyFrame Value="720" KeyTime="0:0:4" KeySpline="0.07,0.39,0.92,0.59"/>
         </DoubleAnimationUsingKeyFrames>
         <DoubleAnimationUsingKeyFrames RepeatBehavior="Forever" BeginTime="0:0:0.6"
                                        Storyboard.TargetName="item4" Storyboard.TargetProperty="RenderTransform.Angle">
             <SplineDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
             <SplineDoubleKeyFrame Value="360" KeyTime="0:0:2" KeySpline="0.07,0.39,0.92,0.59"/>
             <SplineDoubleKeyFrame Value="720" KeyTime="0:0:4" KeySpline="0.07,0.39,0.92,0.59"/>
         </DoubleAnimationUsingKeyFrames>
     </Storyboard>
 </BeginStoryboard>

全部代码:

 <Grid>
     <StackPanel Margin="50">
         <Button Content="开始" Width="120" Height="30">
             <Button.Triggers>
                 <EventTrigger RoutedEvent="Button.Click">
                     <BeginStoryboard>
                         <Storyboard>
                             <DoubleAnimationUsingKeyFrames RepeatBehavior="Forever"
                                                            Storyboard.TargetName="item1" Storyboard.TargetProperty="RenderTransform.Angle">
                                 <SplineDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                                 <SplineDoubleKeyFrame Value="360" KeyTime="0:0:2" KeySpline="0.07,0.39,0.92,0.59"/>
                                 <SplineDoubleKeyFrame Value="720" KeyTime="0:0:4" KeySpline="0.07,0.39,0.92,0.59"/>
                             </DoubleAnimationUsingKeyFrames>
                             <DoubleAnimationUsingKeyFrames RepeatBehavior="Forever" BeginTime="0:0:0.2"
                                                            Storyboard.TargetName="item2" Storyboard.TargetProperty="RenderTransform.Angle">
                                 <SplineDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                                 <SplineDoubleKeyFrame Value="360" KeyTime="0:0:2" KeySpline="0.07,0.39,0.92,0.59"/>
                                 <SplineDoubleKeyFrame Value="720" KeyTime="0:0:4" KeySpline="0.07,0.39,0.92,0.59"/>
                             </DoubleAnimationUsingKeyFrames>
                             <DoubleAnimationUsingKeyFrames RepeatBehavior="Forever"  BeginTime="0:0:0.4"
                                                            Storyboard.TargetName="item3" Storyboard.TargetProperty="RenderTransform.Angle">
                                 <SplineDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                                 <SplineDoubleKeyFrame Value="360" KeyTime="0:0:2" KeySpline="0.07,0.39,0.92,0.59"/>
                                 <SplineDoubleKeyFrame Value="720" KeyTime="0:0:4" KeySpline="0.07,0.39,0.92,0.59"/>
                             </DoubleAnimationUsingKeyFrames>
                             <DoubleAnimationUsingKeyFrames RepeatBehavior="Forever" BeginTime="0:0:0.6"
                                                            Storyboard.TargetName="item4" Storyboard.TargetProperty="RenderTransform.Angle">
                                 <SplineDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                                 <SplineDoubleKeyFrame Value="360" KeyTime="0:0:2" KeySpline="0.07,0.39,0.92,0.59"/>
                                 <SplineDoubleKeyFrame Value="720" KeyTime="0:0:4" KeySpline="0.07,0.39,0.92,0.59"/>
                             </DoubleAnimationUsingKeyFrames>
                         </Storyboard>
                     </BeginStoryboard>
                 </EventTrigger>
             </Button.Triggers>
         </Button>
         <Grid Width="100" Height="100" Margin="0 50 0 0">
             <ContentControl RenderTransformOrigin="0.5,0.5" x:Name="item1">
                 <ContentControl.RenderTransform>
                     <RotateTransform Angle="0"/>
                 </ContentControl.RenderTransform>
                 <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>
             </ContentControl>
             <ContentControl RenderTransformOrigin="0.5,0.5" x:Name="item2">
                 <ContentControl.RenderTransform>
                     <RotateTransform Angle="0"/>
                 </ContentControl.RenderTransform>
                 <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>
             </ContentControl>
             <ContentControl RenderTransformOrigin="0.5,0.5" x:Name="item3">
                 <ContentControl.RenderTransform>
                     <RotateTransform Angle="0"/>
                 </ContentControl.RenderTransform>
                 <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>
             </ContentControl>
             <ContentControl RenderTransformOrigin="0.5,0.5" x:Name="item4">
                 <ContentControl.RenderTransform>
                     <RotateTransform Angle="0"/>
                 </ContentControl.RenderTransform>
                 <Ellipse Fill="Blue" Width="10" Height="10" VerticalAlignment="Bottom"/>
             </ContentControl>
         </Grid>
     </StackPanel>
 </Grid>

不想自己写代码的,可以在Nuget中下载XiaFControl来使用Loading控件。
XiaFControl使用教程链接:
Github:https://github.com/LiuliuMao/XiaFControl
Gitee:https://gitee.com/lm961031/xia-fcontrol
更多WPF知识分享Q群:371769310

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF MVVM模式,可以通过在ViewModel使用ObservableCollection来动态添加自定义控件。ObservableCollection是.NET Framework提供的一个集合类,它能够在集合元素发生变化时自动通知界面进行更新。 首先,在ViewModel声明一个ObservableCollection属性,用于存储自定义控件的集合。然后,在需要添加自定义控件的地方,通过操作ObservableCollection来添加新的控件。ViewModel会自动通知界面进行更新。 接下来,界面需要绑定这个ObservableCollection属性,并使用数据模板来定义如何渲染每个自定义控件。在XAML,可以使用ItemsControl或者ListBox等控件来展示这个集合,并通过绑定将集合和数据模板关联起来。 在这个过程,可以根据需要使用拖放、缩放、旋转等功能。可以参考的示例代码,了解如何实现这些功能。 最后,通过实例化ViewModel,并将其赋值给界面的DataContext属性,从而建立ViewModel和View之间的关联。可以参考的代码。 总结起来,实现在WPF MVVM模式动态添加自定义控件的步骤为: 1. 在ViewModel声明一个ObservableCollection属性,用于存储自定义控件的集合; 2. 在需要添加自定义控件的地方,通过操作ObservableCollection来添加新的控件; 3. 在界面,绑定这个ObservableCollection属性,并使用数据模板来定义如何渲染每个自定义控件; 4. 根据需要使用拖放、缩放、旋转等功能; 5. 实例化ViewModel,并将其赋值给界面的DataContext属性,建立ViewModel和View之间的关联。 希望这个实现思路对你有帮助,如果需要更详细的代码示例,可以参考的文章。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [用WPF mvvm如何动态添加自定义控件问题](https://blog.csdn.net/netyou/article/details/104371498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [WPF Prism MVVM【动态添加控件并可用鼠标、拖动、缩放、旋转】](https://blog.csdn.net/redfox6843/article/details/126117819)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值