开发者

wpf shared ResourceDictionary file parameters?

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..


See this article for Converters with parameters. I assume you define the Converter resource in Resource dictionary.

http://www.switchonthecode.com/tutorials/wpf-tutorial-binding-converters


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜