This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I am trying to create a property editor for images where a button with an ellipse is present and the user can click on it to go out and search for an image to associate with the property. I started out with XAML like this: <DataTemplate x:Key="ImageEditor">< DockPanel DockPanel.Dock="right"><StackPanel Orientation="Horizontal">< Button Click="findImage">...</Button><TextBlock Text="{Binding Value}"></TextBlock></ StackPanel></DockPanel></ DataTemplate>
However, when I click on the button, the findImage method does not fire...Is something trapping that event?
|
|
|
We're not trapping it. I've just run up a test with similar XAML to yours and my Click handler is firing correctly. Is it possible that the method is being called but bailing out early? Sorry if that sounds a stupid question, but it's all I can really think of. The other thing that might be worth a quick check is the findImage location and signature (should be on the same Window class as the DataTemplate, with a signature of Sub findImage(sender As Object, e As RoutedEventArgs)). C# gives me compiler errors if it can't locate the method, or the method is wrong, but I believe you are using Visual Basic and that is sometimes more forgiving (I'd be surprised if it were *that* forgiving, but don't have the VB knowledge to be 100% confident). (Could you have inadvertently created two overloads of findImage, and it's calling the "wrong" one? I've done that before... If you're in VS2008 you can right-click in the XAML and choose Navigate to Event Handler to verify that it really is hooked up to the right place.) If no joy, post a minimal but complete project that reproduces the problem and I will take a look. |
|
|
Ok, got the click - dunno why it wasn't working before but it seems just fine now - How do I associate that button click with an item from the grid? In other words, if I have 5 different items in the grid with "image" properties, how do I know which one had its image button clicked? I tried accessing the Grid's SelectedObject but that was set to nothing... I guess I'm confused a bit on how this is all wired together...
|
|
|
Quick and dirty approach: databind the Button.Tag property to "{Binding Property}". Then ((Button)sender).Tag gets you the Node object associated with the button. Slightly less quick but slightly cleaner approach: use a Command instead of the Click event, and databind the CommandParameter to "{Binding Property}". You can then retrieve the CommandParameter from the ExecutedRoutedEventArgs. (This assumes you are using a PropertyEditor rather than a TypeEditor. If you are using a TypeEditor, then the data context is the object being edited, so just use {Binding}, and it will give you a reference to the object.) How this is all wired together: in a PropertyEditor, the data template operates in the context of an ObjectWrapper<T> (where T is the type of the property). You are usually interested in the Value property of ObjectWrapper<T>, but it also exposes some other properties which can be useful, notably the (slightly misnamed) Property property which gives you the Node (a Node being a grid entry), from where you can navigate to information such as the name, declaring type, data type, etc. (I know this is a bit of a terse explanation but hopefully it gives you a bit more of an idea of what's going on.) |
|