简体   繁体   中英

Why is the Dependency Property not returning its value?

I have a MyUserControl with the following Xaml:

<TextBox Text="{Binding InputValueProperty}" />

In the MyUserControl.xaml.cs I have:

public string InputValue
{
    get { return (string)GetValue(InputValueProperty); }
    set { SetValue(InputValueProperty, value); }
}
public static readonly DependencyProperty InputValueProperty = 
    DependencyProperty.Register("InputValueProperty", typeof(string), 
    typeof(MyUserControl));

In my MainWindow.xaml I create a user control:

<local:MyUserControl InputValue="My Input" />

Later on in my MainWindow.xaml.cs I am trying to access this string. All instances of MyUserControl are contained in a List and I access them with a foreach.

string temp = userControl.InputValue;

This is always null. In my MainWindow.xaml I can see the "My Input" in the text box of the user control but I can't ever seem to get it out of there.

DependencyProperty.Register("InputValueProperty", ...

That should be:

DependencyProperty.Register("InputValue", ...

XAML depends on the registered name of the property, not the name of the property accessor.

It looks like the problem is in your binding. Here's a working example that's modeled off your code with a relative source binding:

Here's the user control:

  public partial class MyUserControl : UserControl
  {
    public MyUserControl()
    {
      InitializeComponent();
    }

    public string InputValue
    {
      get { return (string)GetValue(InputValueProperty); }
      set { SetValue(InputValueProperty, value); }
    }
    public static readonly DependencyProperty InputValueProperty =
        DependencyProperty.Register("InputValueProperty", typeof(string),
        typeof(MyUserControl));
  }
<UserControl x:Class="WpfApplication4.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication4" Height="30" Width="300">
    <Grid>
       <TextBox Text="{Binding Path=InputValue, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyUserControl}}}" />
    </Grid>
</UserControl>

And here's the window:

  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      string text1 = ctrl1.InputValue;
      string text2 = ctrl2.InputValue;
      string text3 = ctrl3.InputValue;
//breakpoint here
    }
  }

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication4" Title="Window1" Height="300" Width="300">
    <Grid>
    <StackPanel>
      <local:MyUserControl x:Name="ctrl1" InputValue="My Input" />
      <local:MyUserControl x:Name="ctrl2" InputValue="2" />
      <local:MyUserControl x:Name="ctrl3" InputValue="3" />
      <Button Click="Button_Click" Height="25" Content="debug"/>
      </StackPanel>
    </Grid>
</Window>

If i throw a breakpoint in the click event i can see the bound values of each of the controls. (if you copy and paste from this be sure to change WpfApplication4 to whatever your project is called.

You need to implement INotifyPropertyChanged on your class that has the property

    public class YourClassThatHasTheInputValuePropertyInIt: INotifyPropertyChanged
    {
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

            public string InputValue
            {
                get { return (string)GetValue(InputValueProperty); }
                set { SetValue(InputValueProperty, value);
                      NotifyPropertyChanged("InputValue"); }
     }
    }

This will allow the binding to pick up the property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM