Graham
Graham

Reputation: 8141

Finding the implementation of a binding in WPF xaml

I don't know WPF much at all, but I have this piece of xaml which has the following:

<Button.InputBindings>
    <MouseBinding Gesture="LeftClick" Command="{Binding Endcommand}" />
</Button.InputBindings>

A search of the whole project finds the following:

public ICommand Endcommand { get; set; }

Endcommand = new ViewModels.DelegateCommand(o => DoEnd());

in two seperate files.

The DoEnd is also in both files.

How do I find out which one is actually being bound to. In short I have already figured this out by trial and error, but was hoping for a better solution for the future.

If I try to Peek definition in the xaml file, nothing happens and if I try to Go to definition I get an error saying "Cannot navigate to definition".

If I try "Find all references" in either of the other two files, I only get what is in the same file.

Upvotes: 2

Views: 825

Answers (4)

Paulus
Paulus

Reputation: 1457

There are good answers here, and here is another approach to try. Change your binding name in the XAML to something like

Command="{Binding EndcommandJunk}"

Then run your program in debug mode, and check out the XAML binding failures window. There you will see that the binding to EndcommandJunk has failed, and it will also give the data context it was trying to bind to.

Upvotes: 0

Kirk Horton
Kirk Horton

Reputation: 408

if the button has been assigned a DataContext then Visual Studio will discover its type's information.

if this is our view model:

public class SomeViewModel
{
    public int SomeInt { get; set; }
    public ICommand SomeCommand { get; set; }
}

and this is our MainWindow where we set the button's data context:

<Window
    x:Class="SO60507853.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SO60507853"
    mc:Ignorable="d"
    Title="MainWindow"
    Height="450"
    Width="800">
    <Grid>
        <Button>
            <Button.DataContext>
                <local:SomeViewModel />
            </Button.DataContext>
            <Button.InputBindings>
                <MouseBinding
                    Gesture="LeftClick"
                    Command="{Binding SomeCommand}"/>
            </Button.InputBindings>
        </Button>
    </Grid>
</Window>

then the properties of SomeViewModel are listed in IntelliSense:

intellisense

and we can also peek or go to their definitions:

peek

Upvotes: 1

Andy
Andy

Reputation: 12276

You can use the live visual tree to see what the datacontext is.

Run your app in debug.

Choose Debug > Windows > Live Visual tree.

Select whatever your view is in the treeview.

Right click and choose properties.

Which should give you another window Live Property Explorer. I think by default that appears on the right over

There's a Datacontext property, amongst others.

This isn't as good as Snoop IMO but you pretty much need to use tools like this if you do any substantial wpf development. Snoop is a free download.

Upvotes: 2

Rekshino
Rekshino

Reputation: 7325

It's difficult to say for sure at design time what is DataContext, because it also can be changed at run time. I would just write for the Button where bindings are:

<Button Content="{Binding}"/>

So I will get the Object.ToString() value of the DataContext like a YourNammespace.YourClass

Upvotes: 4

Related Questions