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

Posted dotNET跨平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF效果第二百零四篇之自定义更新控件相关的知识,希望对你有一定的参考价值。

好久没有更新文章,今天抽空来分享一下最近玩耍的自定义控件;里面包含了自定义控件、依赖属性和路由事件;来看看最终实现的效果:1、先来看看前台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:

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

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、对于更新按钮就需要自定义路由事件了,毕竟是处理控件以外的逻辑了:

5、可以用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"/>

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

以上是关于WPF效果第二百零四篇之自定义更新控件的主要内容,如果未能解决你的问题,请参考以下文章

WPF效果第二百零七篇之EditableSlider

WPF效果第二百零一篇之实现合并单元格

WPF效果第二百零二篇之TreeView带连接线

WPF效果第二百零九篇之下载加密的m3u8

WPF效果第二百一十一篇之TreeView勾选

第二百零四节,jQuery EasyUI,Dialog(对话框)组件