2

我正在尝试运行我在下面附上 的文章“ WPF 中的依赖属性”中的代码。

SetValue(MyDependencyProperty, value);但是应用程序会出现异常中断:

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

内部异常:

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

在此处输入图像描述

为了运行这个应用程序,我应该改变什么?

WPF应用程序代码:

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>

更新:

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

第二个版本有:

  • 完全相同的 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;
    }
  }
4

1 回答 1

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

然后这个验证返回假。由于验证失败,您会收到错误消息“'' is not a valid value for property 'MyProperty'。” 对这些功能进行适当的更改。

于 2013-04-26T06:00:56.430 回答