10

I'm trying to bind my elements in a datatemplate that is define in dictionary. Let's make it simple.

I have a simple class

public class A { public string Data {get;set} } 

I have a simple view that contains a ListBox, with ItemSources is a list of class A :

<ListBox ItemsSource="{Binding AList}"> 

The point is, when I define Itemplate in view directly, bind works :

<ListBox.ItemTemplate> <DataTemplate > <TextBlock Text="{Binding Data}" /> <Rectangle Fill="Red" Height="10" Width="10"/> </DataTemplate> </ListBox.ItemTemplate> 

This works great.

But when I define this ItemTemplate in resource Dictionary, binding doesn't works ?

How can I do that ?

PS : This is a simple example to explain my problem, don't tell me to override toString function to make it works or use classe template, my real case is very more complexe than this.

Thanks for help

3 Answers 3

13

Create a new Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <DataTemplate x:Key="dataTemplate"> <StackPanel> <TextBlock Text="{Binding Data}" /> <Rectangle Fill="Red" Height="10" Width="10"/> </StackPanel> </DataTemplate> </ResourceDictionary> 

In MainWindow.xaml refer it

<Window.Resources> <ResourceDictionary Source="Dictionary1.xaml" /> </Window.Resources> <ListBox Name="lst" ItemTemplate="{StaticResource dataTemplate}"></ListBox> 

MainWindow.cs:

public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { var observable = new ObservableCollection<Test>(); observable.Add(new Test("A")); observable.Add(new Test("B")); observable.Add(new Test("C")); this.lst.ItemsSource = observable; } } public class Test { public Test(string dateTime) { this.Data = dateTime; } public string Data { get; set; } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, I'm doing it by load dictionary in app resources (because it contains data for multiple windows) and use dynamic resource cause it's load at runtime depending of skin. And it doesn't work :(
Hi I moved the ResourceDictionary from Dictionary1.xaml to App.xaml (<Application...><Application.Resources><ResourceDictionary>...</ResourceDictionary></Application.Resources></Application...>) and it works here.
Yeah, you're right. My mistake was because I loaded dictionary into Application.Current.Resources.MergedDictionaries in App() constructor. I move it into OnStartup event and then it works. Thanks
1

You give your DataTemplate a Key so you can use explicitly define your template and reuse your template. You also need to make sure the ItemsControl is a child of the control which loads the dictionary.

<DataTemplate x:Key="ADataTemplate"> <TextBlock Text="{Binding Data}" /> <Rectangle Fill="Red" Height="10" Width="10"/> </DataTemplate> <ListBox ItemsSource="{Binding YourItems}" ItemTemplate="{StaticResource ADataTemplate}" /> 

Note: You can use implicit styling on ListBox, however that would apply the same style to all of your ListBoxes.

1 Comment

That's what I'm doing, but defining DataTemplate in resource dictionary make binding not works...
0

Declare your data template in the Resources section of the current Window/UserControl etc as follows and then reference via static resource declaration:

<Window.Resources> For example... <DataTemplate x:Key="MyTemplate"> <TextBlock Text="{Binding Data}" /> <Rectangle Fill="Red" Height="10" Width="10"/> </DataTemplate> </Window.Resources> <ListBox ItemTemplate="{StaticResource MyTemplate}" /> 

3 Comments

I can't, I have to do it in resource dictionary because my resource dictionary is loaded at runtime depending of selected skin
The Data Template declaration will be the same. <Window.Resources> is just a resource dictionary at the Window level. Declare the data template in the resource dictionary of your choice.
You may want to try DynamicResource instead of StaticResource then if the theme is changed at runtime. This will ensure it is always evaluated but a small performance penalty may result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.