如何更正此示例说明WPF DependencyProperty用法?

时间:2013-04-26 04:44:40

标签: c# wpf debugging data-binding dependency-properties

我正在尝试运行下面附带的文章“Dependency Properties in WPF”中的代码。

但应用程序在SetValue(MyDependencyProperty, value);行中断,但例外情况为:

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

内部异常:

  

{“'在类型上调用构造函数   匹配指定的'_3DP_CallBack_DefaultValue.MainWindow'   绑定约束引发了异常。行号'3'和行   位置'9'。“}

enter image description here

为了运行此应用,我应该更改什么?

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>

更新

上面是一个带有2个以前WPF应用程序的WPF应用程序的3d(增量版)版本,我运行时没有任何错误

第二个版本有:

  • 完全相同的XAML代码
  • 完全相同的C#MainWindow()的构造函数/方法体;
  • class DependencyPropertySample : DependencyObject{}中缺席的方法
    • OnMyPropertyChanged( DependencyObject dObject, DependencyPropertyChangedEventArgs e)
    • OnValidateMyProperty(object myObj)
    • OnCoerceValue(DependencyObject dObject, object val)

public static readonly DependencyProperty MyDependencyProperty = DependencyProperty.Register()不同:

以下是工作第二版DependencyPropertySample的代码:

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);
  }
}

以下是来自应用程序失败的3d版本的DependencyPropertySample的代码

  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;
    }
  }

1 个答案:

答案 0 :(得分:4)

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

此函数在比较后返回string.Empty

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

然后此验证返回false。由于验证失败,您会收到错误“''不是属性'MyProperty'的有效值。”对这些功能进行适当的更改。