Note that there are some explanatory texts on larger screens.

plurals
  1. PODependencyProperty PropertyChangedCallback causes NullReferenceException in XAML
    primarykey
    data
    text
    <p>I've got a subclassed UserControl that is the content for my main window. I added a DepedencyProperty to my usercontrol, of type ResizeMode, and the PropertyChanged callback sets the ResizeMode of the main window to the value correctly. It runs fine. I can set it from the code behind, or from the XAML, and it works correctly. However, when I set it from XAML, the designer throws an Object reference not set to an instance of an object exception, on the code in the PropertyChanged callback that sets the window's resize.</p> <pre class="lang-xml prettyprint-override"><code>&lt;classes:MyUserControl ResizeMode="NoResize"&gt; &lt;...&gt; &lt;/classes:MyUserControl&gt; </code></pre> <p>This is the callback. MainWindow is a reference to the parent window.</p> <pre class="lang-cs prettyprint-override"><code>private static void OnResizeModeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { (o as MyUserControl).MainWindow.ResizeMode = (ResizeMode) e.NewValue; } public ResizeMode ResizeMode { get { return (ResizeMode) GetValue(ResizeModeProperty); } set { SetValue(ResizeModeProperty, value); } } public static readonly DependencyProperty ResizeModeProperty = DependencyProperty.Register("SizeToFit", typeof(ResizeMode), typeof(MyUserControl), new UIPropertyMetadata(ResizeMode.CanResize, new PropertyChangedCallback(OnResizeModeChanged))); </code></pre> <p>I could ignore it, or set it in the code behind, but I don't really understand the reason for this error, and I would prefer to set it in XAML.</p> <p>Can anyone shed some light?</p>
    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