wpf共享ResourceDictionary文件参数?

发布于 2024-09-07 08:25:43 字数 169 浏览 4 评论 0原文

如果所有样式和转换器都存储在共享资源字典文件(styles.xaml)中,并且该文件在各个窗口中使用。

是否可以将参数传递给该文件,并将该参数传播给转换器?

我正在寻找一种传递“origin”类型参数的方法,以便转换器可以知道它们是从哪个位置使用的?只是提示当前哪个窗口/网格正在使用转换器。

if all Styles and Converters are stored in shared resource dictionary file (styles.xaml), and this file is used from various windows.

Is it possible, to pass a parameter to that file, and propagate that parameter to the converters?

I am looking for a way to pass a "origin" type parameter, so that the converters could be aware which place they are being used from? Just a hint of which window/grid is using the converter at the moment..

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

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

发布评论

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

评论(1

日暮斜阳 2024-09-14 08:25:43

我想知道这样的方法是否可行:

在每个 Window 或 UserControl 的声明中添加对 System 命名空间的引用。

     xmlns:sys="clr-namespace:System;assembly=mscorlib"

然后在您的资源部分中进行如下设置:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
            <sys:String x:Key="WinConvertParam">IDTextForThisWindow</sys:String>
    </ResourceDictionary>
</Window.Resources>

您的绑定语法可能如下所示:

{Binding SomeProperty, 
         Converter={StaticResource thatConverterIWrote}, 
         ConverterParameter={StaticResource WinConvertParam}}

...并且您的转换类中的 Convert 或 ConvertBack 方法然后会意识到正在使用它们的 Window,只要您改变 的值 从一个文件到另一个文件。

你怎么认为?

I wonder if something like this will work:

Add a reference to the System namespace in the declaration of each Window or UserControl where you want this.

     xmlns:sys="clr-namespace:System;assembly=mscorlib"

Then in your resources section set things up like this:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
            <sys:String x:Key="WinConvertParam">IDTextForThisWindow</sys:String>
    </ResourceDictionary>
</Window.Resources>

Your binding syntax could then look something like this:

{Binding SomeProperty, 
         Converter={StaticResource thatConverterIWrote}, 
         ConverterParameter={StaticResource WinConvertParam}}

...and your Convert or ConvertBack methods in your conversion classes then become aware of the Window that's using them, provided you vary the value of that <sys:String/> from file to file.

What do you think?

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