WPF效果第二百零四篇之自定义更新控件

好久没有更新文章,今天抽空来分享一下最近玩耍的自定义控件;里面包含了自定义控件、依赖属性和路由事件;来看看最终实现的效果:a5812d83ee659fc3399f4dcecffa3eba.gif1、先来看看前台Xaml布局和绑定:

<Style TargetType="{x:Type Cores:UploadWithProgressControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Cores:UploadWithProgressControl}">
                    <WrapPanel>
                        <Grid>
                            <TextBox Background="#323232"
                                     Style="{StaticResource TriggerTextBox}"
                                     IsReadOnly="True" Text="{TemplateBinding FilePath,Converter={StaticResource FilePathToFileNameConverter}}"
                                     VerticalContentAlignment="Center"/>
                            <Button x:Name="SelectFileButton" Style="{StaticResource OpenFileButton}"
                                    HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,15,5,15"/>
                        </Grid>
                        <Button Content="更新" x:Name="UpdateButton" Margin="10,10,0,10"
                                Style="{StaticResource BaseButton}"
                                IsEnabled="{TemplateBinding CurrentState,Converter={StaticResource UpdateStateEnumToButtonIsEnabledConverter}}"/>
                    </WrapPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

2、后台代码,关键点在TemplatePart和GetTemplateChild:

8de06af269cc151b2e05f5a26bcff73e.png3、对于文件的选择无非就是控件内部依赖属性的处理:

private void SelectFileButton_Click(object sender, RoutedEventArgs e)
 {
     System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog
     {
         Title = "选择要升级的文件",
         Filter = "升级程序(*.bin)|*.bin|所有文件(*.*)|*.*"
     };
     System.Windows.Forms.DialogResult isSelected = openFileDialog.ShowDialog();
     if(isSelected == System.Windows.Forms.DialogResult.OK)
     {
         //选择的文件
         string fileName = openFileDialog.FileName;
         FilePath = fileName;
         CurrentState = UpdateStateEnum.SelectFile;
         OperateType = OperateTypeEnum.SelectFile;
     }
 }

4、对于更新按钮就需要自定义路由事件了,毕竟是处理控件以外的逻辑了:

decfb1f20d1127a25b44d4a08054c83b.png5、可以用Code Snippet快速创建路由事件:

https://andrewharcourt.com/articles/code-snippet-for-wpf-routed-event

6、最后来个调用完事:

<Cores:UploadWithProgressControl VerticalAlignment="Center" HorizontalAlignment="Center"
                                  IsUploading="{Binding FirstData.IsUpload,Mode=TwoWay}"
                                  ProgressValue="{Binding FirstData.ProgressValue,Mode=TwoWay}"
                                  CurrentState="{Binding FirstData.UpdateState,Mode=TwoWay}"
                                  Update="UploadWithProgressControl_Click"/>

最终简单的效果先这样吧1cad4e35d82296b64126ff8f9e0be6f7.png;以后有时间的话,可以再去摸索一下更复杂的效果87f0655266c62fff289417e801890a78.png;编程不息、Bug不止、无Bug、无生活db387707900712c17324ccaefcd2e9f9.png;改bug的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!如果觉得不错,那就伸出您的小手点个赞并关注一下!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值