Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DependencyProperties

I have just realized I've been coercing binding/dependency properties and not really fundamentally understanding the concept.

Heres the dependency property:

public string Problem
{
    get { return (string)GetValue(ProblemProperty); }
    set { SetValue(ProblemProperty, value); }
}

public static readonly DependencyProperty ProblemProperty =
    DependencyProperty.Register(
    "Problem",
    typeof(string),
    typeof(TextBox));

The XAML is as so:

<TextBlock Text="{Binding Path=Problem}"/>

I'm manually setting the Problem property to a value in the constructor of the object but it doesn't update the TextBlock accordingly . . . any ideas? I've tried Mode="OneWay" and Mode="TwoWay" on the binding and it still doesn't work.

I thought this was supposed to work automatically? Or am i fundamentally getting something wrong?

Thanks

like image 762
Dave Avatar asked Nov 25 '08 16:11

Dave


People also ask

What are dependency properties in WPF?

Windows Presentation Foundation (WPF) provides a set of services that can be used to extend the functionality of a type's property. Collectively, these services are referred to as the WPF property system. A property that's backed by the WPF property system is known as a dependency property.

What is dependency property in WPF interview?

In WPF applications, dependency property is a specific type of property which extends the CLR property. It takes the advantage of specific functionalities available in the WPF property system. A class which defines a dependency property must be inherited from the DependencyObject class.

What is a dependency property in XAML?

A dependency property is a specific type of property where the value is followed by a keen property system which is also a part of the Windows Runtime App. A class which defines a dependency property must be inherited from the DependencyObject class.


2 Answers

The problem your having is definitely related to your DataContext. The {Binding} extension needs to know where the property lives that you are binding to. The default location it looks at is the elements DataContext which by default is always set to the DataContext of it's parent element. If you walk the DataContext up the logical tree to the parent window, The DataContext would be null (because the DataContext of your window is null). Therefore your {Binding} on your textblock is saying "Bind my Text property to the Problem roperty of my DataContext...which is null.

There are a couple of ways to solve this. One would be to do just like Jobi mentioned and set the Element property of you binding to point to the Window where the DependencyProperty is defined like this:

<TextBlock Text="{Binding Path=Problem,ElementName=_window}" />

Another option would be to set the DataContext of your Window to point to itself. That way all the elements contained in it's content will all have the DataContext of the window.

<Window ....
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

Now anytime you need to binding to properties defined in the window (like your Problem dependency property), then you can just do this:

<TextBlock Text="{Binding Problem}" />
like image 168
Micah Avatar answered Nov 01 '22 04:11

Micah


You can use ElementName Binding here , element will be the Window itself.

<Window x:Class="WpfToolTip.Window1"
x:Name="_window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Button Click="OnClick" Content="OK" />
        <Button Click="OnCancel" Content="Cancel" />
        <TextBlock Text="{Binding Path=Problem,ElementName=_window}" />
</StackPanel>

like image 2
Jobi Joy Avatar answered Nov 01 '22 04:11

Jobi Joy