WPF/Silverlight:部分模板化自定义控件

发布于 2024-10-05 12:50:30 字数 713 浏览 3 评论 0原文

大家好,

我们可以以这样的方式编写自定义控件,以便稍后我们可以部分重新模板化它吗?意味着,不替换整个 ControlTemplate,我们想要更改/覆盖它的某些外观?

当然,一种方法是:从 generic.xaml 复制 ControlTemplate 代码,部分修改它的外观,然后在您的 xaml 中使用它,如下所示:

<MyCustomControl>
       <MyCustomControl.Template>
              <ControlTemplate>

                    <!-- paste the copied and modified code from generic.xaml here-->

              </ControlTemplate>
        </MyCustomControl.Template>

        <!--other code-->

</MyCustomControl>

但这种方法的问题是,我们使 xaml <强>太冗长。我真的不喜欢它。我正在寻找一些解决方案,可以避免污染我使用自定义控件的 xaml。

感谢期待!

.

Hello guys,

Can we write our custom controls in such a way that later on we can re-template it partially? Means, without replacing the entire ControlTemplate, we want to change/override some of it's look and feel?

One way, of course, is this : copy the ControlTemplate code from generic.xaml, modify it's look and feel partially, and use it in your xaml, as shown below:

<MyCustomControl>
       <MyCustomControl.Template>
              <ControlTemplate>

                    <!-- paste the copied and modified code from generic.xaml here-->

              </ControlTemplate>
        </MyCustomControl.Template>

        <!--other code-->

</MyCustomControl>

But the problem with this approach is that, we make our xaml too verbose. And I seriously don't like it. I'm looking for some solution that may save me from polluting the xaml where I'm using my custom control.

Thanks in anticipation!

.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲸落 2024-10-12 12:50:30

我同意 Martin 所说的大部分内容,即 ControlTemplates 在 WPF 中几乎是全有或全无的东西,当我得出这个结论时,我自己也感到非常沮丧。

但是,如果您编写自己的自定义控件,则可以使其更加灵活,为什么编写或提供对控件进行模板化的功能。例如,您可以查看较新的 WPF DataGrid 控件,该控件能够模板化控件的不同部分。其他带标题的内容控件通常有一个单独的内容模板,并且控件的标题部分可以替换其中一个,但不能替换另一个。

I agree with most of what Martin says about ControlTemplates being pretty much an all or nothing thing in WPF and I was pretty frustrated myself when I came to this conclusion.

If your writing your own custom control however you can make it more flexible why composing or offering the ability to template pices of the control. For an example you can look at the newer WPF DataGrid control which has the ability to template different pieces of the control. Other headered content controls often have a separate template for the content and the header portion of the control making it possible to replace one but not the other.

倾城°AllureLove 2024-10-12 12:50:30

不幸的是,修改控件模板是一个全有或全无的提议。正如您所指出的,当您修改复杂的控件模板时,XAML 会变得非常冗长。

一种方法(无论如何,这是非常明显的)是从小的可组合部分构建 UI,希望使控件模板更易于使用。

您可以通过使用 Style.BasedOn 来重用样式属性。不幸的是,这并不能解决你的问题。

Unfortunately modifying a control template is an all or nothing proposition. As you point out your XAML becomes very verbose when you modify a complex control template.

One approach (which is quite obvious anyway) is to build your UI from small composable parts hopefully making the control templates easier to work with.

You have the possibility of reusing styles by using the Style.BasedOn Property. Unfortunately, this doesn't solve your problem.

妳是的陽光 2024-10-12 12:50:30

我也面临同样的问题,进行一个小的编辑需要我复制并粘贴整个默认控件模板。

不过,您可以定义多个资源字典并使用 将标记与控件模板分开。

您的 xaml 标记将更加清晰,如下所示:

 <Style>
        <Setter Property="Template" Value="{StaticResource SomeRandomTemplate}" />
 </Style>

I'm also facing the same problem where making one small edit requires me copying and pasting the entire default control template.

You can however define multiple resource dictionaries and use <ResourceDictionary.MergedDictionaries> to separate your markup from the control templates.

Your xaml markup will be much cleaner and look something like this:

 <Style>
        <Setter Property="Template" Value="{StaticResource SomeRandomTemplate}" />
 </Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文