2013-04-26 27 views
2

我试图运行下面附加的文章“Dependency Properties in WPF”中的代码。在线SetValue(MyDependencyProperty, value);如何更正此示例说明WPF DependencyProperty用法?

但应用程式的空档,出现异常:

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

内部异常:

{“_3DP_CallBack_DefaultValue.MainWindow '符合指定 '构造对 类型调用'绑定约束抛出了一个异常。'行号 '3' 和线 位置 '9'。“}

enter image description here

我应该怎么才能运行这个程序改变?

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> 

更新:

上面是与2以前的WPF应用三维(增量)版本的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()缺席方法不同:

这里是与c从工作的第二个版本的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); 
    } 
} 

这里是DependencyPropertySample类的从失败的应用的3D版本的代码:

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

我没有看到你在任何地方指定你想绑定的属性。使用mybinding.Path =“MyProperty” – 2013-04-26 05:17:31

+0

@ Erti-ChrisEelmaa,'mybinding.Path =“MyPropertyPath”;'给出编译错误'“无法将源类型'字符串'转换为目标类型'System.Windows.PropertyPath''.无论如何,不清楚为什么同一个应用程序的先前版本在没有任何路径设置的情况下没有任何问题请参阅我的问题 – Fulproof 2013-04-26 05:53:26

回答

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'的有效值。”对这些功能进行适当的更改。

+0

@ Manish中的更新,在更改了'DependencyProperty.Register中的新CoerceValueCallback(OnCoerceValue)'后(...)'给出编译错误''OnCoerceValue'没有重载匹配委托'System.Windows.CoerceValueCallback'“ – Fulproof 2013-04-26 06:42:21

+0

你对OnCoerceValue做了什么改变? – Manish 2013-04-26 06:47:29

+0

如果你只是想成功运行这个代码,改变if(val。 ToString()。CompareTo(“Test”)== 1)to if(val.ToString()。CompareTo(“Test”)== -1)。ie -1而不是1 – Manish 2013-04-26 06:54:14