WPF 自定义控件的三种方式
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">WPF控件<span style="color: black;">能够</span><span style="color: black;">经过</span>数据模型(DataTemplate)、样式(Style)、控件模板(ControlTemplate)和触发器(Trigger)等机制减少创建新控件的需要。 <span style="color: black;">然则</span>,某些场景下,<span style="color: black;">咱们</span>确实需要创建新的控件。此时,理解 WPF<span style="color: black;">区别</span>控件的创建<span style="color: black;">办法</span>就<span style="color: black;">显出</span>非常重要。 WPF <span style="color: black;">供给</span>3个用于创建控件的<span style="color: black;">办法</span>,<span style="color: black;">每一个</span><span style="color: black;">办法</span>都<span style="color: black;">供给</span><span style="color: black;">区别</span>的灵活度,下面分别进行介绍。</span></span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">1 基于UserControl 创建</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">创建控件最简单一个<span style="color: black;">办法</span><span style="color: black;">便是</span>基于UserControl 类进行继承。此时,<span style="color: black;">咱们</span><span style="color: black;">能够</span>将WPF中现有组件添加到 UserControl 画布上来,并将各组件进行命名,<span style="color: black;">这般</span><span style="color: black;">能够</span>在后台进行组件<span style="color: black;">拜访</span>和<span style="color: black;">运用</span>事件处理程序。 UserControl <span style="color: black;">能够</span>利用丰富内容、样式和触发器的优点。 <span style="color: black;">然则</span>,继承自 UserControl的控件,将<span style="color: black;">没法</span><span style="color: black;">运用</span> DataTemplate 或 ControlTemplate 来自定义UI外观。</span></span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">2 基于Control 创建</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">基于Control类创建自定义控件的<span style="color: black;">办法</span> ,<span style="color: black;">能够</span><span style="color: black;">运用</span>模板定义UI外观。<span style="color: black;">况且</span><span style="color: black;">能够</span>将后台<span style="color: black;">规律</span>和前端样式展现上进行分离。 <span style="color: black;">另一</span>,这种<span style="color: black;">办法</span>创建的自定义控件,还支持<span style="color: black;">运用</span>命令和绑定来完成<span style="color: black;">关联</span>动作,实现类似事件的效果。最后,控件<span style="color: black;">能够</span>重新定义ControlTemplate和DataTemplate来自定义UI外观。控件支持<span style="color: black;">区别</span>的主题。</span></span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">3 基于 FrameworkElement创建</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">通常</span><span style="color: black;">来讲</span>,基于 UserControl 或 Control 创建的自定义控件<span style="color: black;">就可</span>完成业务<span style="color: black;">需要</span>,<span style="color: black;">然则</span>,在<span style="color: black;">有些</span>特殊<span style="color: black;">状况</span>下,简单的元素组合<span style="color: black;">不可</span>满足自定义控件的UI外观<span style="color: black;">需求</span>。此时,基于FrameworkElement 创建自定义控件是一个很好的<span style="color: black;">选取</span>。</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">基于FrameworkElement创建控件,一方面<span style="color: black;">能够</span><span style="color: black;">经过</span>重写的 OnRender <span style="color: black;">办法</span>进行UI的直接绘制。 另一方面,<span style="color: black;">能够</span><span style="color: black;">经过</span>自定义元素组合来可视化编写组件的外观。</span></span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">4依赖属性</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">WPF <span style="color: black;">能够</span><span style="color: black;">经过</span>设置控件的属性来更改其外观和<span style="color: black;">行径</span>。其中的依赖属性<span style="color: black;">能够</span>让自定义控件执行以下操作:</span></span></span></p><span style="color: black;"><span style="color: black;"><span style="color: black;">在样式中设置该属性。</span></span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">将该属性绑定到数据源。</span></span></span><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">运用</span>动态资源<span style="color: black;">做为</span>该属性的值。</span></span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">对该属性进行动画处理。</span></span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">如果控件的属性支持以上任一功能,应将该属性实现为依赖属性。 下面给出一个微软官方文档的示例程序:</span></span></span></p><span style="color: black;"><span style="color: black;">///</span> <span style="color: black;"><summary></span></span>
<span style="color: black;"><span style="color: black;">///</span> Identifies the Value dependency property.</span>
<span style="color: black;"><span style="color: black;">///</span> <span style="color: black;"></summary></span></span>
<span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">readonly</span>DependencyProperty ValueProperty =
DependencyProperty.Register(<span style="color: black;">"Value"</span>, <span style="color: black;">typeof</span>(<span style="color: black;">decimal</span>), <span style="color: black;">typeof</span>(NumericUpDown),
<span style="color: black;">new</span> FrameworkPropertyMetadata(MinValue, <span style="color: black;">new</span>PropertyChangedCallback(OnValueChanged),<span style="color: black;">new</span> CoerceValueCallback(CoerceValue)));
<span style="color: black;"><span style="color: black;">///</span> <span style="color: black;"><summary></span></span>
<span style="color: black;"><span style="color: black;">///</span> Gets or sets the value assigned to the control.</span>
<span style="color: black;"><span style="color: black;">///</span> <span style="color: black;"></summary></span></span>
<span style="color: black;">public</span> <span style="color: black;">decimal</span> Value
{
<span style="color: black;">get</span> { <span style="color: black;">return</span> (<span style="color: black;">decimal</span>)GetValue(ValueProperty); }
<span style="color: black;">set</span> { SetValue(ValueProperty, <span style="color: black;">value</span>); }
}
<span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">static</span> <span style="color: black;">object</span> <span style="color: black;">CoerceValue</span>(<span style="color: black;">DependencyObject element,<span style="color: black;">object</span> <span style="color: black;">value</span></span>)</span>
{
<span style="color: black;">decimal</span> newValue = (<span style="color: black;">decimal</span>)<span style="color: black;">value</span>;
NumericUpDown control = (NumericUpDown)element;
newValue = Math.Max(MinValue, Math.Min(MaxValue, newValue));<span style="color: black;">return</span> newValue;
}
<span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">OnValueChanged</span>(<span style="color: black;">DependencyObject obj, DependencyPropertyChangedEventArgs args</span>)</span>{
NumericUpDown control = (NumericUpDown)obj;
RoutedPropertyChangedEventArgs<<span style="color: black;">decimal</span>> e = <span style="color: black;">new</span> RoutedPropertyChangedEventArgs<<span style="color: black;">decimal</span>>(
(<span style="color: black;">decimal</span>)args.OldValue, (<span style="color: black;">decimal</span>)args.NewValue, ValueChangedEvent);
control.OnValueChanged(e);
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此示例代码来自官网</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://docs.microsoft.com/zh-cn/dotnet/desktop/wpf/controls/control-authoring-overview?view=netframeworkdesktop-4.8 ,其中定义一个名为 ValueProperty的依赖属性(DependencyProperty ),<span style="color: black;">经过</span>调用</p>DependencyProperty.Register向属性系统注册属性名<span style="color: black;">叫作</span>Value,其中<span style="color: black;">包括</span>了三个核心信息:<span style="color: black;"><span style="color: black;"><span style="color: black;">属性的名<span style="color: black;">叫作</span>Value</span></span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">属性的类型decimal</span></span></span><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">持有</span>属性的类型NumericUpDown</span></span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">属性元数据信息(FrameworkPropertyMetadata)</span></span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中,属性的元数据<span style="color: black;">包括</span>属性的默认值, CoerceValueCallback 和 PropertyChangedCallback 。 CoerceValue <span style="color: black;">保证</span> Value 大于或等于 MinValue 且<span style="color: black;">少于</span>或等于 MaxValue。<span style="color: black;">另一</span>,PropertyChangedCallback 回调<span style="color: black;">办法</span>为 OnValueChanged ,来处理属性值变化的<span style="color: black;">关联</span><span style="color: black;">规律</span>,后面<span style="color: black;">经过</span></p>RoutedPropertyChangedEventArgs创建了一个路由事件,并<span style="color: black;">经过</span>control.OnValueChanged(e)来进行触发。
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">原文链接:</strong></p><strong style="color: blue;">http://click.aliyun.com/m/1000296154/
</strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">本文为阿里云原创内容,未经<span style="color: black;">准许</span>不得转载。</strong></span></p>
外贸B2B平台有哪些?
页:
[1]