Fulproof
Fulproof

Reputation: 4636

How to correct this sample illustrating WPF DependencyProperty usage?

I am trying to run the code from the article "Dependency Properties in WPF" I've attached below.

But app breaks on line SetValue(MyDependencyProperty, value); with exception:

System.Windows.Markup.XamlParseException   
"'' is not a valid value for property 'MyProperty'."

Inner exception:

{"'The invocation of the constructor on type '_3DP_CallBack_DefaultValue.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'."}

enter image description here

What should I change in order to run this app?

The code of WPF app:

namespace _3DP_CallBack_DefaultValue
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      DependencyPropertySample dpSample = new DependencyPropertySample();
      dpSample.MyProperty = "Dependency Property Test";//???

      Binding mybinding = new Binding("MyProperty");
      mybinding.Mode = BindingMode.OneWay;
      mybinding.Source = dpSample;
      BindingOperations.SetBinding(MyTextblock, TextBox.TextProperty, mybinding);
    }
  }
  public class DependencyPropertySample : DependencyObject
  {
    //Register Dependency Property
    public static readonly DependencyProperty MyDependencyProperty 
      = DependencyProperty.Register
           (
               "MyProperty", typeof(string), typeof(DependencyPropertySample), 
               new PropertyMetadata
                 (
                    "Test", 
                    new PropertyChangedCallback(OnMyPropertyChanged), 
                    new CoerceValueCallback(OnCoerceValue)
                 ), 
                 new ValidateValueCallback(OnValidateMyProperty) 
           );

    public string MyProperty
    {
      get
      {
        return (string)GetValue(MyDependencyProperty);
      }
      set
      {
//***************************
//breaking on the following line trying to set any string value   
// in this case "Dependency Property Test"
        SetValue(MyDependencyProperty, value);
      }
    }

    public static void OnMyPropertyChanged(DependencyObject dObject,
          DependencyPropertyChangedEventArgs e)
    {
      MessageBox.Show(e.NewValue.ToString());
    }
    public static string OnCoerceValue(DependencyObject dObject, object val)
    {
      if (val.ToString().CompareTo("Test") == 1)
      {
        return val.ToString();
      }
      return string.Empty;
    }
    public static bool OnValidateMyProperty(object myObj)
    {
      if (myObj.ToString() == string.Empty)
        return false;
      return true;
    }
  }
}

XAML:

<Window x:Class="_3DP_CallBack_DefaultValue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Label Content="Enter String:" Grid.Row="0"
            VerticalAlignment="Center" />
        <TextBox Text="" Name="MyTextblock" Height="25"
             Width="150" HorizontalAlignment="Right" />
    </Grid>
</Window>

Update:

Above was a 3d (incremental) version of WPF app with 2 previous WPF app, I've run without any errors

The second version had :

and the public static readonly DependencyProperty MyDependencyProperty = DependencyProperty.Register() was different:

Here is the code of DependencyPropertySample class from working 2nd version:

public class DependencyPropertySample : DependencyObject
{
  //Register Dependency Property
  public static readonly DependencyProperty MyDependencyProperty = 
           DependencyProperty.Register
              ("MyProperty", typeof(string), typeof(DependencyPropertySample));
public string MyProperty
{
  get
  {
    return (string)GetValue(MyDependencyProperty);
  }
  set
  {
    SetValue(MyDependencyProperty, value);
  }
}

Here is the code of DependencyPropertySample class from failing 3d version of app:

  public class DependencyPropertySample : DependencyObject
  {
    //Register Dependency Property
    public static readonly DependencyProperty MyDependencyProperty 
      = DependencyProperty.Register
           (
               "MyProperty", typeof(string), typeof(DependencyPropertySample), 
               new PropertyMetadata
                 (
                    "Test", 
                    new PropertyChangedCallback(OnMyPropertyChanged), 
                    new CoerceValueCallback(OnCoerceValue)
                 ), 
                 new ValidateValueCallback(OnValidateMyProperty) 
           );

    public string MyProperty
    {
      get
      {
        return (string)GetValue(MyDependencyProperty);
      }
      set
      {
        SetValue(MyDependencyProperty, value);
      }
    }
    public static void OnMyPropertyChanged(DependencyObject dObject,
          DependencyPropertyChangedEventArgs e)
    {
      MessageBox.Show(e.NewValue.ToString());
    }
    public static string OnCoerceValue(DependencyObject dObject, object val)
    {
      if (val.ToString().CompareTo("Test") == 1)
      {
        return val.ToString();
      }
      return string.Empty;
    }
    public static bool OnValidateMyProperty(object myObj)
    {
      if (myObj.ToString() == string.Empty)
        return false;
      return true;
    }
  }

Upvotes: 2

Views: 450

Answers (1)

Manish
Manish

Reputation: 510

public static string OnCoerceValue(DependencyObject dObject, object val)
    {
      if (val.ToString().CompareTo("Test") == 1)
      {
        return val.ToString();
      }
      **return string.Empty;**
    }

This function returns string.Empty after the comparison

public static bool OnValidateMyProperty(object myObj)
        {
            if (myObj.ToString() == string.Empty)
                **return false;**
            return true;
        }

And then this Validation returns false. Since the validation fails, you get the error "'' is not a valid value for property 'MyProperty'." Make appropriate changes to these functions.

Upvotes: 4

Related Questions