Reputation: 1654
I have clickable image/rectangle controls throughout my application and prefer to wire them in code:
clickableimagecontrol.MouseUp += MouseUp_Handler
I am struggling to understand how to do the same with datatemplates. I basically need to find the spot where the template is applied to each item, so I can insert my hooks.
I know WPF recommends using Commands of some type, but that requires more boiler-plate code, not to mention that image/rectangles don't support commands so I'd have to redo the whole thing with styled button controls instead.
Is this possible?
Upvotes: 2
Views: 1169
Reputation: 3689
Please see workable solution:
private void ElementKeyUp(object source, KeyEventArgs args)
{
throw new NotImplementedException();
}
private DataTemplate GetDataTemplate()
{
var result = new DataTemplate();
var factory = new FrameworkElementFactory(typeof(ListViewItem));
var handler = new KeyEventHandler(ElementKeyUp);
factory.AddHandler(KeyUpEvent, handler);
result.VisualTree = factory;
return result;
}
Upvotes: 1