Sam Andy

I have a data template as resource in a ResourceDictionary. but inside code when I use FindResource("myTaskTemplate") I get resource not found

<ResourceDictionary

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="clr-namespace:MyControls.Windows.Controls"

>

<DataTemplate x:Key="myTaskTemplate">

<Image Source="{Binding Text}" Width="100" Height="100"/>

</DataTemplate>

.

.

.

.

</ResourceDictionary>



Re: Windows Presentation Foundation (WPF) data template AS Resource in ResourceDictionary

Bragi

On which object are you calling FindResource Try something like this:

Code Block

App.Current.FindResource("myTaskTemplate") as DataTemplate;


Offcourse, the resource directory that contains the template must also be accessible. Which resource dictionary is it Are you using a seperate one, if so, the system probably can't find the dict. For instance, if you are trying to find the template in Window1, but the templat is defined in a seperate resource dict called dict1, try something like this in the resource section of window1:


Code Block

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="dict1.xaml"/>
</ResourceDictionary.MergedDictionaries>


<!--put any extra resources here-->


</ResourceDictionary>
</Window.Resources>



This will include your seperate resources into the dict of the window.





Re: Windows Presentation Foundation (WPF) data template AS Resource in ResourceDictionary

Sam Andy

my resource dict is in generic.xaml file. i need to use this template in a custome control and I dont have Window.Resources





Re: Windows Presentation Foundation (WPF) data template AS Resource in ResourceDictionary

Bragi

Ahh, generic.xaml. I have found that strings as resource names doesn't work very well with this type of dict, although I don't know why exactly this is. ResourceKey objects do seem to work with this. Try something like:

-creates a ResourceKey descendent (SystemResourceKeyID is an enum with an enum item for each of resource key):

Code Block

internal class SystemThemeKey : ResourceKey

{

// Fields

private SystemResourceKeyID _id;

private static Assembly _presentationFrameworkAssembly;

// Methods

internal SystemThemeKey(SystemResourceKeyID id)

{

this._id = id;

}

public override bool Equals(object o)

{

SystemThemeKey key = o as SystemThemeKey;

if (key != null)

{

return (key._id == this._id);

}

return false;

}

public override int GetHashCode()

{

return (int)this._id;

}

public override string ToString()

{

return this._id.ToString();

}

// Properties

public override Assembly Assembly

{

get

{

if (_presentationFrameworkAssembly == null)

{

_presentationFrameworkAssembly = typeof(DockingControl).Assembly;

}

return _presentationFrameworkAssembly;

}

}

internal SystemResourceKeyID InternalKey

{

get

{

return this._id;

}

}

}

-creates a static prop on the control class (with a static field to back it up):

Code Block

static private SystemThemeKey splitterStyleKey;

public static ResourceKey SplitterStyleKey

{

get

{

if (splitterStyleKey == null)

{

sSplitterStyleKey = new SystemThemeKey(SystemResourceKeyID.Splitter);

}

return splitterStyleKey;

}

}

use your key:

Code Block

<Style x:Key="{x:Static self:MyControl.SplitterStyleKey}" TargetType="{x:Type v:DockSplitter}">

<Setter Property="Control.Background" Value="Transparent"/>

<Setter Property="Control.IsTabStop" Value="False"/>

</Style>

This should work.





Re: Windows Presentation Foundation (WPF) data template AS Resource in ResourceDictionary

Sam Andy

its really works now. but one more thing, I did that in the generic.xaml. is it possible to overide this key in a demo for example. how can use this key from outside the control to set the data template





Re: Windows Presentation Foundation (WPF) data template AS Resource in ResourceDictionary

Sam Andy

I used SetResourceReference and its fixed my problem.