WPF、用户控件或数据模板

发布于 2024-08-12 13:32:48 字数 1556 浏览 6 评论 0原文

最近我尝试在我的应用程序中重用一些 UI 元素。当我开始使用 WPF 编程时,我被告知 DataTemplate 是重用 UI 元素的最佳方式。您可以为数据实体定义模板并在任何地方使用它。听起来非常好。 但是,我也发现了一些缺点,特别是与 UserControl 相比时。

  1. 您不能重用在另一个窗口或用户控件中定义的数据模板。例如,如果 UserDataTemplate 是在 WindowA.xaml 中定义的,则不能在 WindowB.xaml 中使用它。解决方案可能是将 DataTemplate 作为资源放入全局资源字典中。
  2. DataTemplate 很难有一些代码隐藏。如第 1 项所述,如果将 DataTemplate 放入 ResourceDictionary 中,则默认情况下没有地方放置代码。我用谷歌搜索了这个问题,是的,我找到了一个让 ResourceDictionary 有一个 cs 文件的技巧。但它还有另一个问题。
  3. DataTemplate的另一个问题是你必须清楚DataTemplate本身的实例和DataTemplate内容的实例之间的区别。一个 DataTemplate 将只有一个“DataTemplate 实例”,并且可能有许多 DataTemplate 内容的实例。让我用一个例子来解释一下:

    <前><代码><数据模板>><网格.资源>> 公共部分类CodeBehind { 点 m​​ousePos = new Point(); 私有无效OnMouseLeftButtonDown(对象发送者,MouseButtonEventArgs e) { mousePos = e.Pos...; } }

结果将是:User1只会有一个实例,但是一旦应用DataTemplate就会创建一个User2实例,这意味着如果多次应用datatemplate,User2将有很多实例。 然而,与 UserControl 不同,“mousePos”字段不会有很多副本。如果 DataTemplate 被应用 100 次,mousePos 将不会有 100 个副本,这意味着 100 个 Grid 将同时使用唯一的一个 mousePos 字段,这可能会导致问题。 在 UserControl 中,您定义的字段将仅由控件使用。 100 个 UserControl 实例将有 100 个字段副本。

也许我使用 DataTemplate 的方式错误。任何评论表示赞赏。

最好的问候,

扎克

Recently I'm trying to reuse some UI elements in my application. When I started programming with WPF I'm told that DataTemplate is the best way to reuse UI elements. You can define a template for your data entity and use it everywhere. It sounds very good.
However, I also found some disadvantages, especially when it is compared with UserControl.

  1. You cannot reuse a DataTemplate defined in another Window or UserControl. For example, if UserDataTemplate is define in WindowA.xaml, you cannot use it in WindowB.xaml. The solution might be that put the DataTemplate as a resource in a global Resource Dictionary.
  2. DataTemplate is hard to have some code behind. As mentioned in item 1, if you put the DataTemplate in a ResourceDictionary, there is no place to put your code by default. I googled the problem and yes, I found a trick to make the ResourceDictionary have a cs file. But it still has another problem.
  3. Another problem of DataTemplate is that you must be clear with the difference between the instance of DataTemplate itself and the instances of the content of DataTemplate. A DataTemplate will have only one "instance of DataTemplate", and may have many instances of the content of the DataTemplate. Let me explain it with an Example:

    <DataTemplate>
            <DataTemplate.Resources>
                    <my:User x:key="User1"/>
            </DataTemplate.Resources>                
            <Grid MouseLeftButtonDown="OnMouseLeftButtonDown">
                    <Grid.Resources>
                            <my:User x:key="User2"/>
                    </Grid.Resources>
            </Grid>        
    </DataTemplate>
    
    
    public partial class CodeBehind
    {
             Point mousePos = new Point();
    
            private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
            {
                    mousePos = e.Pos...;
            }
    }
    

The result will be that : User1 will only have one instance, however, a User2 instance will be created once the DataTemplate is applied, which means User2 will have many instances if the datatemplate is being applied many times.
However, unlike UserControl, the field "mousePos" will NOT have many copies. If the DataTemplate is being applied 100 times, the mousePos won't have 100 copies, which mean the 100 Grids will use the only one mousePos field as the same time, which may cause problems.
In UserControl, the field you defined will only be used by the control. 100 UserControl instances will have 100 field copies.

Maybe I'm using DataTemplate in the wrong way. Any comment is appreciated.

Best regards,

Zach

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

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

发布评论

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

评论(3

故事未完 2024-08-19 13:32:48

从概念上讲,数据模板和用户控件解决两个不同的问题。它们实际上不能互换,因此您的比较并不准确。

数据模板就是将视觉样式应用于数据类型。通常这意味着我有自己的 .NET 类,名为 Foo,并且我想给它一个视觉样式。我将通过创建数据类型为 Foo 的 DataTemplate 来完成此操作。

然后,我可以将此 DataTemplate 放置在我的应用程序中(例如在 App.XAML 中),并且无论使用什么位置,我都会将我的视觉样式应用于我的数据对象 Foo。通常,这意味着您将看到一个 ContentControl,其 Content 属性绑定到 Foo 类型的属性。

另一方面,UserControls 都是关于 XAML 的组织。用户控件有助于组织您希望在具有与其相关的行为和功能的应用程序中重复使用的 XAML 块。这超出了 DataTempate 的功能。

DataTemplate 与一种 DataType 绑定并显示该类型的视觉效果。 UserControl 可以由多个 DataType 组成,并且可以包含自定义行为。

话虽这么说,我很少发现需要用户控件。我到处使用 DataTemplates 来建模我的数据并通过数据绑定和 MVVM 模式实现我的行为。

Conceptually DataTemplates and UserControls solve two different problems. They are not really interchangeable so your comparisons are not really accurate.

DataTemplates are all about applying a visual style to a DataType. Typically what this means is I have my own .NET class called Foo and I want to give it a visual style. I would do this by creating a DataTemplate with a DataType of Foo.

I can then place this DataTemplate in my application (say in App.XAML) and I will have my visual style applied to my data object Foo wherever it is used. Often this means you will see a ContentControl that has a Content property bound to a property of type Foo.

UserControls on the other had are all about organization of XAML. A user control helps in organizing chunks of XAML that you want to re-use throughout your application that has behaviors and functionality tied to it. This is more than what a DataTempate will do.

A DataTemplate is tied to one DataType and display a visual for that type. A UserControl can be composed of multiple DataTypes and can include custom behaviors.

That being said, I very rarely find the need for a UserControl. I use DataTemplates all over to model my data and implement my behaviors though data bindings and the MVVM pattern.

傲世九天 2024-08-19 13:32:48

就我个人而言,我创建一个 UserControl,然后从中创建一个 DataTemplate。对我来说,这有以下优点:

  1. 可以跨窗口使用,只需重新定义 DataTemplate 部分。
  2. 可以使用代码隐藏(我知道,我知道,但有些事情使用代码隐藏要容易得多,我不认为基于教条使我的代码不必要地复杂化)。
  3. XAML 设计器支持。

Personally, I create a UserControl and then make a DataTemplate from that. This has to following advantages, to me:

  1. Can use across windows, only by re-defining the DataTemplate part.
  2. Can use code-behind (I know, I know, but some things are just so much easier using code-behind, I don't see the point in unnecessary complicating my code based on dogma).
  3. XAML designer support.
↙厌世 2024-08-19 13:32:48

关于 2。

我想说 DataTemplates 并不是设计用来与代码隐藏一起使用的。在大多数情况下,您只能使用数据绑定和命令来连接模型及其表示之间的逻辑。没有代码隐藏也有利于应用程序的单元测试。

About 2.

I'd say that DataTemplates are not designed to be used with code-behind. You can most of the case only use DataBinding and Commands to wire the logic between your model and its representation. Not having code-behind also facilitate the unit tests of you application.

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