0
votes

is it possible to use the Resources I defined in a in another UserControl, without using an extra file for a ResourceDictionary.

I have a UserControl "ViewCostumers" and in I define a DataTemplate with a ListView of all the customers. Now I write a UserCotnrol "ViewOverview" in which the same ListView shall appear. I would like keep the LsitView in the file "ViewCostumer" if somehow possible.

Thanks in advance

Edit: What I tried to do is that I keep the DataTempalte/ListView in the original file. Then I'd like to reference that file (wich is a UserControl) from another file (like a resource dictionary, only ViewCostumer is a UserControl:

< ResourceDictionary> 
< ResourceDictionary.MergedDictionaries>         
< ResourceDictionary Source="View/ViewCostumer.xaml" /> 
< /ResourceDictionary.MergedDictionaries>
< /ResourceDictionary>
2
resources are referenced by their keys, so any place you want to use a resource just put the key in your code to ask for it ... there's no limit on the number of times you can use the same key.War

2 Answers

1
votes

is it possible to use the Resources I defined in a in another UserControl, without using an extra file for a ResourceDictionary.

No, it is not. After all, this is exactly what ResourceDictionaries are used for.

I want to keep the Resources in the original file (which is a UserControl).

You can't and you shouldn't if you intend to use the resources in any other control than this particular UserControl. It simply makes no sense to define a resource in a UserControl if you intend to use this resource in any other control.

What you should do is to define the common resource in a ResourceDictionary that you then merge into both controls as suggested by @Ephraim.

0
votes

You can move the Resources to the Application scope.

Check out Creating and Consuming Resource Dictionaries in WPF and Silverlight specifically the Moving Resources to Application Scope.

The idea is to move the resources into the Application.xaml so that in can be used throughout your entire application.

Application.xaml

<Application 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="ResourceDictionaryDemo.App"
    xmlns:my="clr-namespace:System;assembly=mscorlib">
    <Application.Resources>
        <LinearGradientBrush x:Key="formBackground" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Blue" Offset="0" />
            <GradientStop Color="#460000FF" Offset="1" />
        </LinearGradientBrush>
        <my:String x:Key="applicationTitle">Resource Dictionary Demo</my:String>
        <my:Double x:Key="applicationTitleFontSize">18</my:Double>
        <SolidColorBrush x:Key="applicationTitleForeground">Yellow</SolidColorBrush>
    </Application.Resources>
</Application>