Question

I have a DataTemplate defined in a xaml file that I want to access via C# code. Can anyone please tell me how can I access it? I added a new ResourceDictionary file and its name is Dictionary1.xaml. I have a data template such as:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DataTemplate x:Key="mytemplate">
        <TextBlock Text="Name:" Background="Blue"/>
    </DataTemplate>
</ResourceDictionary>

not I have a ListBox called listBox1 and I want to assign it to it's Itemtemplate property but I'm not getting how can i do it?

Was it helpful?

Solution

Where exactly are you defining it?

If you define it in the ResourceDictionary of your object, then

Application.Current.Resources[typeof(yourDataTemplateTargetType)] 

should work. If you are defining it as a member of something else, like say, an ItemsControl, you need to get a handle to the ItemsControl instance and call the ItemTemplate property.

Edit: Ok, I think we're getting somewhere. So you are defining a ResourceDictionary in its own file. Before you can use it in your UI and access it from your code behind, you need to merge that ResourceDictionary into your application. Are you doing this?

If you are, then the next step is to get this resource. Each FrameworkElement has a method called FindResource. This method is great because it walks up the ResourceDictionary tree and attempts to locate the resource with the key. So, if you want to access this resource from a UserControl, you can do the following in the code behind:

FindResource(typeof(yourDataTemplateTargetType));

If this doesn't work for you, please show us exactly how you are declaring this resource dictionary and how it is getting merged into your application's resources.

OTHER TIPS

Since Application.Current was null in my case, I've ended up using this:

    var myResourceDictionary = new ResourceDictionary();
    myResourceDictionary.Source =
        new Uri("/DllName;component/Resources/MyResourceDictionary.xaml",
                UriKind.RelativeOrAbsolute);  

and then getting the specified key I needed by using myResourceDictionary["KeyName"] as TypeOfItem

(source)

If you for example have a template for Button in your resource dictionary in the App.xaml file you can access it using the following code:

Application.Current.Resources[typeof(Button)]

You can access a resource dictionary you added to your project as follows:

var rd = new ResourceDictionary();
rd.Source = new Uri("ms-appx:///Dictionary1.xaml");

Then you can access a resource stored in the resource dictionary like so:

someObject.Property = rd["mytemplate"];

NOTE:
You will have to modify the URI to the resource dictionary according to the location you created it relative to the project's base directory.

Any of the above approaches work getting the resource based on the location, if you are following MVVMm I would recommend doing it this way:

  1. create a Service like ProvideDataTemplateService, (to create a service usual inherit from Behavior )
  2. Use Container of Your choice to inject this service where you would like to have aces to DataTemple.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top