Wpf:将参数传递给 ResourceDictionary [英] Wpf: Passing Parameters To A ResourceDictionary

查看:50
本文介绍了Wpf:将参数传递给 ResourceDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将参数传递给资源字典?我认为我可以附加一个代码来指定事件处理程序,但不幸的是,我还需要从事件处理程序访问对父控件的引用.我相信,可以通过在 xaml 中为资源字典指定 x:Class 属性来附加代码隐藏,然后在同一文件夹中创建一个类,其文件名类似于 [资源字典名称].xaml.cs.

Is there a way to pass parameters to a resource dictionary? I think that I can attach a code behind for the purpose of specifying event handlers, but unfortunately, I also need to access a reference to the parent control from the event handlers. The codebehind, I believe, can be attached by specifying an x:Class attribute for the resource dictionary in xaml, and then creating a class in the same folder, the filename for which is something like [resource dictionary name].xaml.cs.

目的是将我在单个树视图控件中使用的四个分层数据模板的代码分开.树视图的 xaml 看起来有点长和难看,所以我希望将其分解为四个资源词典.欢迎提出任何想法!

The purpose is to seperate the code for four hierarchical data templates that I'm using in a single treeview control. The xaml for the treeview is getting a bit long and ugly to look at, so I was hoping to break it down into four resource dictionaries. Any thoughts are welcome!

安德鲁

推荐答案

资源词典听起来有点奇怪.资源词典都是关于共享实例的——它们让您可以使用来自多个地方的某事物的单个实例(例如样式、模板、画笔或其他任何东西).它们并不是真正用于划分 UI 以简化单个 Xaml 文件的机制.

Resource dictionaries sound like a slightly peculiar way to do this. Resource dictionaries are all about sharing instances - they let you use a single instance of something (e.g. a style, a template, a brush, or whatever) from multiple places. They're not really a mechanism for dividing your UI up to simplify individual Xaml files.

将过于复杂的 Xaml 文件拆分为几个更易于管理的较小文件的常用机制是用户控制.(资源字典合并在你已经有一个资源字典并且它太大的时候开始发挥作用.但你通常不会为了开始拆分而引入资源字典.相反,资源字典倾向于鼓励过大的 Xaml 文件,这就是为什么首先必须发明字典合并!)

The usual mechanism for splitting overly complicated Xaml files up into a few, more manageable smaller files is the user control. (Resource dictionary merging comes into play when you already have a resource dictionary, and it's got too big. But you wouldn't normally introduce a resource dictionary just to start splitting things up. On the contrary, resource dictionaries tend to encourage overly large Xaml files, which is why dictionary merging had to be invented in the first place!)

大多数时候,当我定义一个数据模板时,我只让它包含一个用户控件.如果这变得更复杂,我会将用户控件拆分为更多用户控件.

Most of the time, when I define a data template, I make it contain nothing but a single user control. And if that becomes more complex, I'd split that user control up into more user controls.

从您的描述来看,您的 Xaml 文件似乎变大了,因为其中有四个大型分层数据模板.如果您将每个模板的主体转换为用户控件,那么您的四个模板现在将变得非常简单 - 如下所示:

From your description, it sounds like your Xaml file has become large because you've got four large hierarchical data templates in there. If you took the body of each template and turned it into a user control, your four templates would now become very simple - something like this:

<HierarchicalDataTemplate x:Key="t1" ItemsSource="{Binding Path=Children}">
    <loc:TreeItemTypeOne />
</HierarchicalDataTemplate>

并且您很可能不再需要将这些模板放入单独的文件中.但由于每个模板的内容现在都在用户控件中,因此您可以将代码隐藏起来.

and you'd most likely no longer need to put those templates into separate files. But because the guts of each template is now in a user control, that gives you a place to put your codebehind.

您提到需要对父控件的引用.这让我很担心 - 这听起来像是你的代码隐藏中有太多的代码.但一次做一件事...您可以通过在您的用户控件上定义一个名为 ParentControl 的依赖属性来解决该问题,然后将其放入模板中:

You mention needing a reference to the parent control. That worries me - it makes it sound like you have too much code in your codebehind. But one thing at a time... You could solve that problem by defining a dependency property called ParentControl on your user control, and then put this in the template:

<loc:TreeItemTypeOne
    ParentControl="{Binding RelativeSource=
        {RelativeSource AncestorType=loc:ParentControlType}}" />

但坦率地说,一旦我发现自己处于需要它的位置,我就会问自己:我是如何将自己置于一个似乎有必要的位置,我能做些什么来解决这个问题?

But frankly, as soon as I find myself in a position where I need this, I ask myself: how did I get myself into a position where that seemed necessary, and what can I do to fix that?

这篇关于Wpf:将参数传递给 ResourceDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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