kick it on DotNetKicks.com   Shout it  

Xaml: Binding to the current DataContext

Sometimes instead of binding to a property of the current DataContext you want to bind to the actual DataContext itself. For example, I am using DelegateCommand<T> from Composite Application Library and needed to bind a command to perform an action on the current item in a ListBox.

<UserControl x:Name="ViewRoot">
    <ListBox ItemSource="{Binding Path=MyCollection}">
        <Button Content="Delete" cmd:Click.Command="{Binding ElementName=ViewRoot, Path=DataContext.Delete}
            cmd:Click.CommandParameter={??????} />
    </ListBox>
</UserControl>

I just wasn’t sure what to put here. Every example I’ve read binds to a property of the current DataContext, but how do I bind to the context itself. Here’s the definition of my command:

public class MyViewModel {
    public DelegateCommand<MyObject> Delete { get; private set; }
    
    public MyViewModel() {
        Delete = new DelegateCommand<MyObject>(this.DeleteExecute,
                            this.DeleteCanExecute);
    }
    
    private void DeleteExecute(MyObject item) {
        item.Delete();
    }
 
    private void DeleteCanExecute(MyObject item) {
        true;
    }
}

So how do you set cmd:Click.CommandParameter so that it passes the current instance of MyObject to the DeleteExecute and DeleteCanExecute methods? It turns out you can pass an empty Binding expression (or an expression without setting a value for ‘Path’) just like this:

<Button ... cmd.Click.CommandParameter="{Binding}" />

This wasn’t obvious to me – I was thinking there was some sort of notation. I tried a couple different variations of setting ‘Path’ to something like “DataContext” or “.” (like XPath), but couldn’t find anything else. Then I was looking up something else and found this post. Under the first bullet the sample workaround shows the same syntax as above. Once I tried it that worked. Hope this helps.

Tags: , , ,

kick it on DotNetKicks.com   Shout it  

Feedback

# re: Xaml: Binding to the current DataContext

Gravatar
This is a good piece of writing mate for this particular topic. I was wondering if I could use this write-up on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away.
7/2/2010 12:21 PM | drug treatment

# re: Xaml: Binding to the current DataContext

Gravatar I always try to do the best job I can in the shortest amount of time. I have been running a restaurant and want to get a new website up and going. I am trying to learn so that I can program it myself. 7/27/2010 12:32 PM | scotsman ice machine

Post a comment





 

Please add 7 and 7 and type the answer here:

 

 

Copyright © Mark J. Miller