Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to correct this sample illustrating WPF DependencyProperty usage?
    primarykey
    data
    text
    <p>I am trying to run the code from the article "<a href="http://miteshsureja.blogspot.ru/2011/07/dependency-properties-in-wpf.html" rel="nofollow noreferrer">Dependency Properties in WPF</a>" I've attached below. </p> <p>But app breaks on line <code>SetValue(MyDependencyProperty, value);</code> with exception: </p> <pre><code>System.Windows.Markup.XamlParseException "'' is not a valid value for property 'MyProperty'." </code></pre> <p>Inner exception: </p> <blockquote> <p>{"'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'."}</p> </blockquote> <p><img src="https://i.stack.imgur.com/Qye4h.jpg" alt="enter image description here"></p> <p>What should I change in order to run this app? </p> <p>The code of WPF app: </p> <pre><code>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; } } } </code></pre> <p>XAML: </p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Label Content="Enter String:" Grid.Row="0" VerticalAlignment="Center" /&gt; &lt;TextBox Text="" Name="MyTextblock" Height="25" Width="150" HorizontalAlignment="Right" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <h1>Update:</h1> <p>Above was a 3d (incremental) version of WPF app with 2 previous WPF app, I've run without any errors </p> <p>The second version had : </p> <ul> <li>exactly the same XAML code</li> <li>exactly the same C# <code>MainWindow()</code>'s body of constructor/method; </li> <li>the absent methods in <code>class DependencyPropertySample : DependencyObject{}</code> <ul> <li><code>OnMyPropertyChanged( DependencyObject dObject, DependencyPropertyChangedEventArgs e)</code> </li> <li><code>OnValidateMyProperty(object myObj)</code> </li> <li><code>OnCoerceValue(DependencyObject dObject, object val)</code> </li> </ul></li> </ul> <p>and the <code>public static readonly DependencyProperty MyDependencyProperty = DependencyProperty.Register()</code> was different: </p> <p>Here is the code of <code>DependencyPropertySample</code> class <strong>from working 2nd version:</strong> </p> <pre><code>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); } } </code></pre> <p>Here is the code of <code>DependencyPropertySample</code> class <strong>from failing 3d version of app</strong>: </p> <pre><code> 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload