更改WPF的StackPanel模板 [英] Changing WPF StackPanel template

查看:101
本文介绍了更改WPF的StackPanel模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义样式面板( StyledStackPanel ),这等同于普通的StackPanel,具有自定义样式。
为例 - 如果样式是简单地添加下划线,通过写:

 < StyledStackPanel> 
<! - 用户的内容放在这里 - >
< / StyledStackPanel>



我想获得相同的外观,如果我写的:

 <&StackPanel的GT; 
<! - 用户的内容放在这里 - >
< / StackPanel的>
< BORDER身高=1BorderBrush =蓝/>

如果的StackPanel 控制,我可以代替的DataTemplate 对照。然而,它继承了面板它继承 FrameworkElement的
有没有办法来改变的StackPanel


解决方案

有没有办法来设置一个面板控件模板,但你有几种选择:




  1. 创建自定义/具有StackPanel中作为一个孩子的用户控件。


  2. 从StackPanel中继承和覆盖的OnRender绘制自定义样式(您可能还需要重写的MeasureOverride和ArrangeOverride周围的面板的孩子加一点空间,为您。绘图


  3. 从面板直接继承和覆盖的OnRender,和的MeasureCore ArrangeCore - 如果你需要使用的面板是一个特定的方式,你并不需要所有StackPanel中的选项,这也许因为StackPanel的布局算法的最简单的方法是简单的。


  4. 从装饰继承(如果你在代码中绘图)或ContentControl中(如要使用控件模板),并用它扭曲你的面板,当你使用它:

     < L:PanelStyle> 
    < StackPanel的> ..< StackPanel的>
    < / L:PanelStyle>


  5. 写为ItemsControl的风格,ItemsControl的有可以设置的ControlTemplate,基本上包装了一个面板(默认情况下它是一个垂直的StackPanel)。




我会选择4去,如果你需要访问的StackPanel的属性或5,如果你不这样做。


I am trying to create a custom style panel (StyledStackPanel) which is identical to regular StackPanel, that has a custom style. For example - if the style is simply to add an underline, by writing:

<StyledStackPanel>
    <!--User content goes here-->
</StyledStackPanel>

I would like to receive the same appearance as if I wrote:

<StackPanel>
    <!--User content goes here-->
</StackPanel>
<Border Height="1" BorderBrush="Blue"/>

If StackPanel was a Control, I could have replace the DataTemplate of that control. However, it inherits Panel which inherits FrameworkElement.
Is there a way to change the template of a StackPanel?

解决方案

There is no way to set a ControlTemplate for a panel, but you have several options:

  1. Create a custom/user control that has the StackPanel as a child.

  2. Inherit from StackPanel and override OnRender to draw your custom style (you may also need to override MeasureOverride and ArrangeOverride to add a little space around the panel's children for your drawing.

  3. Inherit directly from Panel and override OnRender, MeasureCore and ArrangeCore - if you need to use the panel is a specific way and you don't need all of StackPanel's option this maybe the easiest way since the StackPanel layout algorithm is simple.

  4. Inherit from Decorator (if you do your drawing in code) or ContentControl (if you want to use ControlTemplate) and warp your panel with it when you use it:

         <l:PanelStyle>
             <StackPanel> .. <StackPanel>
         </l:PanelStyle>
    

  5. Write the style for ItemsControl, ItemsControl has a ControlTemplate that you can set and basically wraps a panel (by default it's a vertical StackPanel).

I would go with option 4 if you need access to the StackPanel's properties or 5 if you don't.

这篇关于更改WPF的StackPanel模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆