WPF Bind to class member in code behind
Asked Answered
P

4

32

Pretty simple question, but can't seem to find a complete answer on here...

I need to databind in xaml to a property of a class member in codebehind.

<Window x:Class="Main">
    <customcontrol Name="View" IsChecked="{Binding ElementName=RecordProp, Path=IsViewChecked}" />
...

Where the code behind looks like:

class Main 
{    
    ...
    private Record _record;
    public Record RecordProp
    {
      get { return _record; }
    }
    ...
}


class Record
{
  public bool IsViewChecked
  {
    get; set;
  }
}

What I've got now doesn't work, what am I doing wrong?

Paulsen answered 2/3, 2010 at 21:33 Comment(0)
S
28

Path needs a Source to go against (Source, DataContext, RelativeSource, ElementName). ElementName can only be used to refer to elements declared in XAML by their x:Name. Try this instead to point to your Window as the source:

IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=RecordProp.IsViewChecked}"
Scientific answered 2/3, 2010 at 21:49 Comment(0)
P
30

What I'm seeing here is that your window's class name is Main, that you've added a RecordProp property to it, and that you're now trying to bind to the IsChecked property of the element named RecordProp. I think you're a little confused about how names work.

Adding the x:Name attribute to a XAML element creates a field in the window class with that name. This allows you to reference named elements in your code, and it probably has led you to think that binding can do the same thing.

But that's not how binding finds named elements. The x:Name attribute also takes the object that the XAML element creates and registers it under that name in the window's namescope. (See MSDN's article on XAML namescopes.) That's what binding looks at to resolve element names. Since you're not ever adding the object to the namescope, setting the ElementName property on a binding won't find it.

There are a couple of things you could conceivably do. If you really want to bind to a property of the window, you can give the window a name and bind to the property using a property path:

<Window x:Name="MainWindow" x:Class="Main">
...
   <customcontrol Name="View" IsChecked="
                 {Binding ElementName=MainWindow, 
                  Path=RecordProp.IsViewChecked}" />

Even simpler is to just set the data context in the constructor:

DataContext = this;

Once you do that, you can just bind to the RecordProp property (and any other property of the window) like this:

<customControl Name="View" IsChecked={Binding RecordProp.IsChecked}/>

Of course, that won't work if you need the window's data context to be set to something else.

Another possibility is to implement the property like this:

public Record RecordProp 
{
  get { return (Record)Resources["RecordProp"]; }
  set { Resources["RecordProp"] = value; }
}

You can bind to this using (for instance) Binding {DynamicResource RecordProp}, Path=IsChecked". Since it's a dynamic resource, if something external to the window sets the window's RecordProp property, the bindings to it will refresh - which is something that won't happen if you just make RecordProp a property (unless you implement change notification).

Perineuritis answered 3/3, 2010 at 0:7 Comment(2)
One note, DataContext should be set not in the Window constructor, but in Loaded event handler. Setting this in a constructor didn't work for me for ObservableCollection when I filled it inside the Loaded event.Kilovolt
Instead of setting the Windows's DataContext from code, you can simply add this to the Windows's XAML: DataContext="{Binding RelativeSource={RelativeSource Self}}Mall
S
28

Path needs a Source to go against (Source, DataContext, RelativeSource, ElementName). ElementName can only be used to refer to elements declared in XAML by their x:Name. Try this instead to point to your Window as the source:

IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=RecordProp.IsViewChecked}"
Scientific answered 2/3, 2010 at 21:49 Comment(0)
M
7

I believe I have a simpler answer than the ones stated so far. Simply add this to the window declaration (the very first tag) in XAML:

x:Name="this"

Then you can databind like this:

<customcontrol Name="View" IsChecked="{Binding ElementName=this, Path=RecordProp.IsViewChecked}" />

I checked to see if C# complains that there already is a "this," and it didn't, I guess because they both refer to the exact same object.

This is the solution I used when I ran into the same problem, and I found it to be very intuitive to use.

Montane answered 18/4, 2011 at 16:41 Comment(2)
Is there a reason not to do it this way? I've tried the other solutions without success. This is the only way I've been able to get it working with my noob C# skills.Emf
If you downvote, please comment on why you think this is a bad solution.Montane
C
3

Data Binding won't work against private fields. It's meant for public properties instead. Try exposing the value _record publically and bind to that instead.

Reference - http://msdn.microsoft.com/en-us/library/ms743643.aspx

Constant answered 2/3, 2010 at 21:34 Comment(2)
The public property had no affect.Paulsen
@user258651 I think u need to update the binding context as well for the Main Window to have it default back to the Main class. Been awhile since I looked at that though.Constant

© 2022 - 2024 — McMap. All rights reserved.