简体   繁体   中英

How to use IValueConverter implementation from another file?

I have IValueConverter implementation - IconExtractor . I use it this way:

<Image Source="{Binding Converter={StaticResource iconExtractor},
                        ConverterParameter=c:\\Windows\\System32\\shell32.dll|72}"/>

But for it to work, the IconExtractor must reside in the code-behind-file of the XAML, eg: my main window is MainWindow.xaml , so the class IconExtractor must be in MainWindow.xaml.cs . I cannot place it elsewhere?

Your IValueConverter class can be anywhere you want, including in another assembly. Based on your use, you need to have a resource somewhere accessible from your MainWindow class. An easy way is to have it in your window resources.

<MainWindow.Resources>
    <my:IconExtractor x:Key="iconExtractor"/>
</MainWindow.Resources>

You can also use the resources in App.xaml , since its resources are accessible from all XAML based classes in your project. Using App.xaml appeals to me since you can avoid duplicating effort if more than one window in your project will use IconExtractor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM