简体   繁体   English

自定义控件模板,显示和WPF

[英]Custom Control Templates, Display and WPF

I come from a Windows Forms background and I am a little confused on what would be considered the best practice for controlling the display of a control. 我来自Windows窗体背景,我对控制控件显示的最佳实践有点困惑。 I am working on a Control that I want some "Eye Candy" on. 我正在制作一个控制器,我想要一些“眼睛糖果”。 In WinForms I would override the Paint Event to do any kind of visual programming. 在WinForms中,我会覆盖Paint事件来进行任何类型的可视化编程。 I understand that the main reason WPF was conceived, is to separate the Action from the Display. 我知道WPF构思的主要原因是将Action与Display分开。

Here is the part that I am unsure about. 这是我不确定的部分。 Say I want to draw some simple lines in the "Background" of the Control. 假设我想在控件的“背景”中绘制一些简单的线条。 Would it be considered best practice to add the lines into the Template or do the Drawing of the Lines in the Code Behind as I would do in WinForms? 将这些行添加到模板中或者在WinForms中执行代码后面的行绘制是最佳实践吗?

For Example say I am developing a Control named CustomControl1 and want to draw a line the entire width of the control that is centered vertically like: 例如,我说我正在开发一个名为CustomControl1的Control,并希望绘制一条直线居中的控件的整个宽度,如:

CustomControl1

Would it be better to do the line in the Code Behind or in the template? 在Code Behind或模板中执行该行会更好吗?

I can do it entirely in xaml like so: 我可以完全在xaml中完成它,如下所示:

<Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Padding="{TemplateBinding Padding}"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                        <Grid HorizontalAlignment="Left" Width="{Binding ActualWidth}">

                            <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Aqua">
                                <Path Stretch="Fill" Stroke="Black" StrokeThickness="1" Data="M 0,0.5 H 1" />
                            </Border>

                            <TextBlock 
                                    HorizontalAlignment="Center"
                                    Text="Some Text" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I am not sure if this is the "Best Practice" or if there another way that I should use all together? 我不确定这是“最佳实践”还是我还有其他方法可以一起使用? Is there one reason to use one over the other? 是否有一个理由使用一个而不是另一个? Are there any performance gains/losses? 是否有任何绩效收益/亏损?

Anything static should be in the xaml. 任何静态都应该在xaml中。 For example, if you know this line will always start at this position, with this size, put it in the xaml. 例如,如果您知道此行总是从此位置开始,使用此大小,请将其放在xaml中。 But if the line is dynamic, like you can have one or many lines, with angles, curves, different colors ect... You might want to initialize it yourself and set properties in your user control. 但是如果线条是动态的,就像你可以有一条或多条线条,角度,曲线,不同的颜色等...你可能想要自己初始化它并在你的用户控件中设置属性。

IMO,your code is fine. IMO,你的代码很好。 No one would ever do " do the Drawing of the Lines in the Code Behind as I would do in WinForms" for this kind of requirements in WPF. 对于WPF中的这种要求,没有人会像在WinForms中那样“在代码背后绘制线条”。

Controlling the display of control using ControlTemplate and Style is best and recommended approach in WPF. 使用ControlTemplate和Style控制控件的显示是WPF中的最佳和推荐方法。 Also, if you need the Controls in code,you can get the Controls defined in ControlTemplate using GetTemplateChild method inside OnApplyTemplate override of your custom control. 此外,如果您需要代码中的控件,则可以使用OnApplyTemplate覆盖自定义控件中的GetTemplateChild方法获取ControlTemplate中定义的控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM