关键词不能为空

当前您在: 主页 > 英语 >

WPF 自定义 ItemsControl 控件

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-02-27 19:35
tags:

-

2021年2月27日发(作者:pdpc)


该控件叫



Summary,


主要是一些汇总信息的显示,


有几个地方用,


之前都是 分散到各个


XAML


文件里,不统一。




本人


WPF


新手,



XAML


了解不多,


做这个软件基本上都是用


CM



界面布局用



AvalonDock



由于缺乏相关经验,


又没有一个能问得上的人指导,


写这个控件费了我很长时间


(啥时有空啥时

动动)。



之前主要做一些功能方面的,

< br>没有心思美化界面,


现在虽然还有很多功能没写,


但是基 本上够自


己用了,放下心思来做一些界面上的东西,要不然何苦选择

WPF





先看一下图:







CustomControl


由< /p>


4


部分组成:



大标题,小标题,值





Detail



虽然细分这么多,但实质上还是一个列表类的控件,所以选择继承自



ItemsControl.



做 这个控件的时候,遇到了一些详细的问题不知道怎么解决,


Google/Bing


都没有找到我要了


解的,


Baidu


更不用提了,漫天的转载,


Copy.



1,


类似



ComboBox




DisplayMemberPath


如何弄?



既然都自定控件了,当然是想让它适用不同场景,



不能局限于某一个实体类,最好是能像



DisplayMemberPath





ValueMemberPath


这样的属性。



这里,我定义了:



ItemTitlePathProperty




ItemValuePathProperty


来处理。




2


,有了上面两个依赖属性,但是还不足以处理更多的场景,最好是能有不同的



Template




这里我定义了:



TitleTemplate / ValueTemplate



DetailTemplate.



第一个问题,只需定义一个简单的



DataTemplate


,然后用



TemplateBinding


即可做到。


但是要和第二个问题结合在一起,


就牵扯到模板切换





数据切换的问题,


因为第一问题所用的


数据被指定为某实体类的某个属性,第二个问题的数据要指 定为整个实体。



解决第一个问题,需要重写



PrepareContainerForItemOverride


方法,第二个需要重写



OnApplyTemplate



不多说了,怕误导大家,毕竟,我懂的不多,上代码,供参考:



模板文件:






1



<


ResourceDictionary


xmlns


=





2























xmlns:x


=





3



























xmlns:local


=





4























>





5






6






7







<


DataTemplate


x:Key


=





8











<


TextBlock


Text


=


{Binding}


Foreground


=



/>





9








Da taTemplate


>




10





11







<


DataTemplate


x:Key


=




12











<


Border


Horizonta lAlignment


=


Background


=



CornerRadius


=




13















<


TextBlock


Text


=


{Binding}


Padding


=



Foreground


=



/>




14












Bo rder


>




15








Da taTemplate


>




16





17





18







<


Style


TargetType


=


{x:Type local:Summary}





19











<


Setter


Property


=




20















<



>




21



















<


ControlTemplate


TargetType


=


{x:Type local:Summary}





22























<


Border


Backgroun d


=


{TemplateBinding


Background}





23































Bor derBrush


=


{TemplateBinding


BorderBrush}





24































Bor derThickness


=


{TemplateBindin g


BorderThickness}






25































>




26





27



























<


StackPanel


Height


=




28































<


Border


Padding


=



CornerRadius


=


Background


=




29



































<


TextBlock


Text


=


{Binding


RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type


local:Summary}}, Path=Title}



FontSize


=


Foreground


=



Horiz ontalAlignment


=



/>




30
































Border


>




31































<


Border


Background


=



CornerRadius


=


Padding


=




32



































<


ItemsPresenter


/>




33
































Bo rder


>




34




























St ackPanel


>




35





36
























Bo rder


>




37




















Co ntrolTemplate


>




38

















>




39












Setter


>




40








Style


>




41





42







<


Style


TargetType


=


{x:Type local:SummaryItem}





43











<


Setter


Property


=




44















<



>




45



















<


ControlTemplate


TargetType


=


{x:Type

local:SummaryItem}





46























<


Border


x:Name


=




47































Padding


=




48































Bor derBrush


=


{TemplateBinding


BorderBrush}





49































Bor derThickness


=


{TemplateBindin g


BorderThickness}





50





51





52



























<


ound


>




53































<


SolidColorBrush


/>




54




























ou nd


>




55





56



























<


Sta ckPanel


>




57































<


Doc kPanel


>




58



































<


ContentControl


x:Name


=





59



















































Content


=


{TemplateBi nding Title}






60



















































Con tentTemplate


=


{StaticResource SummaryTitleTemplate}





61



















































/>




62





63



































<


ContentControl


x:Name


=





64



















































Content


=


{TemplateBi nding Value}






65



















































Con tentTemplate


=


{StaticResource SummaryValueTemplate}






66



















































Width


=




67




















































=




68



















































HorizontalAlign ment


=




69



















































/>




70





71
































DockPanel


>




72































<


ContentControl


Margin


=



x:Name


=


ContentTe mplate


=


{TemplateBinding DetailTemplate}




/>




73




























St ackPanel


>




74





75



























<


rs


>




76































<


EventTrigger


RoutedEvent


=




77



































<


BeginStoryboard


>




78







































<


Storyboard


>




79











































<


ColorAnimation


To


=


Duration


=




80























































Name


=




81























































Property


=




82








































Storyboard


>




83




































BeginStoryboard


>




84
































EventTrigger


>




85































<


EventTrigger


RoutedEvent


=




86



































<


BeginStoryboard


>




87







































<


Storyboard


>




88











































<


ColorAnimation


To


=


Duration


=




89























































Name


=




90























































Property


=




91








































Storyboard


>




92




































BeginStoryboard


>




93
































EventTrigger


>




94




























rs


>




95





96





97
























Border


>




98




















ControlTemplate


>




99

















>



100












Se tter


>



101








St yle


>



102




103




ResourceDictionary


>









1



using


System;




2



using


c;




3



using


Model;




4



using





5



using





6



using





7



using


s;




8



using


ls;




9



using




10



using


nts;



11



using




12



using




13



using


g;



14



using


tion;



15



using




16





17



namespace


ls {



18







public



class


Summary : ItemsControl {



19





20





21











public



static


DependencyProperty TitleProperty =


er(



,


t ypeof


(


string


),


typeof


(Summary));



22




-


-


-


-


-


-


-


-



本文更新与2021-02-27 19:35,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/675544.html

WPF 自定义 ItemsControl 控件的相关文章

  • 余华爱情经典语录,余华爱情句子

    余华的经典语录——余华《第七天》40、我不怕死,一点都不怕,只怕再也不能看见你——余华《第七天》4可是我再也没遇到一个像福贵这样令我难忘的人了,对自己的经历如此清楚,

    语文
  • 心情低落的图片压抑,心情低落的图片发朋友圈

    心情压抑的图片(心太累没人理解的说说带图片)1、有时候很想找个人倾诉一下,却又不知从何说起,最终是什么也不说,只想快点睡过去,告诉自己,明天就好了。有时候,突然会觉得

    语文
  • 经典古训100句图片大全,古训名言警句

    古代经典励志名言100句译:好的药物味苦但对治病有利;忠言劝诫的话听起来不顺耳却对人的行为有利。3良言一句三冬暖,恶语伤人六月寒。喷泉的高度不会超过它的源头;一个人的事

    语文
  • 关于青春奋斗的名人名言鲁迅,关于青年奋斗的名言鲁迅

    鲁迅名言名句大全励志1、世上本没有路,走的人多了自然便成了路。下面是我整理的鲁迅先生的名言名句大全,希望对你有所帮助!当生存时,还是将遭践踏,将遭删刈,直至于死亡而

    语文
  • 三国群英单机版手游礼包码,三国群英手机单机版攻略

    三国群英传7五神兽洞有什么用那是多一个武将技能。青龙飞升召唤出东方的守护兽,神兽之一的青龙。玄武怒流召唤出北方的守护兽,神兽之一的玄武。白虎傲啸召唤出西方的守护兽,

    语文
  • 不收费的情感挽回专家电话,情感挽回免费咨询

    免费的情感挽回机构(揭秘情感挽回机构骗局)1、牛牛(化名)向上海市公安局金山分局报案,称自己为了挽回与女友的感情,被一家名为“实花教育咨询”的情感咨询机构诈骗4万余元。

    语文