Hi,
I'm trying to make data template using a template selector. There is the code of my class :
public class ModelTemplateSelector : DataTemplateSelector
{
public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
{
Type type = item.GetType();
Type listGeneric = typeof(IMiniView<>);
Type listConcrete = listGeneric.MakeGenericType(new Type[] { type });
var control = typeof(Core)
.GetMethod("GetInstance", new Type[] { })
.MakeGenericMethod(listConcrete)
.Invoke(this, null) as FrameworkElement;
var dataTemplate = new DataTemplate();
var controlFactory = new FrameworkElementFactory(control.GetType());
dataTemplate.VisualTree = controlFactory;
return dataTemplate;
}
}
Indeed, I get T Type of the item and I create a IMiniView(of T) Type. Then, The GetInstance method return an instance of a class that exposes this interface, using the composition container of the MEF. I create a FrameworkElementFactory that I put in the VisualTree of the data template I return to my control container (i.e. a listbox).
This code is working. But what I wanna do is add an mouse binding on the control.
In XAML, you can do it using this code (with a button for instance) :
<DataTemplate>
<Button>
<Button.InputBindings>
<MouseBinding Command="{Binding MyCommand}" MouseAction="LeftDoubleClick" />
</Button.InputBindings>
</Button>
</DataTemplate>
But I want to do it by code. I can't do control.InputBindings.Add(...) because FrameworkElementFactory only get a Type, not an instance of a class.
So if you have any idea,
Thanks for your answers.