bridget
bridget

Reputation: 33

How can I initialize a OneWayToSource dependency property?

I have a dependency property in my control which is bound OneWayToSource to the view model. I can't figure out how to initialize it with a non-static value. If I try to initialize it in the control's constructor, the property value gets changed to the desired value, but for some reason is immediately changed back. Consider this code:

ButterflyControl.xaml.cs

using System;
using System.Diagnostics;
using System.Windows;

namespace OneWayToSourceTest
{
    public partial class ButterflyControl
    {

        public ButterflyControl()
        {
            InitializeComponent();
            RefreshAction = Refresh;
        }

        public static readonly DependencyProperty RefreshActionProperty = DependencyProperty.Register(
            name: nameof(RefreshAction),
            propertyType: typeof(Action),
            ownerType: typeof(ButterflyControl),
            typeMetadata: new PropertyMetadata(null, RefreshActionPropertyChanged));

        public Action RefreshAction
        {
            get => (Action)GetValue(RefreshActionProperty);
            set => SetValue(RefreshActionProperty, value);
        }

        private static void RefreshActionPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            // For debugging purposes, observe changed to the RefreshAction property.
            Debug.WriteLine("RefreshAction changed from '{0}' to '{1}'", (e.OldValue as Action)?.Method.Name, (e.NewValue as Action)?.Method.Name);
        }

        private void Refresh()
        {
            // Refresh code goes here.
        }

    }
}

ButterflyControl.xaml

<UserControl x:Class="OneWayToSourceTest.ButterflyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" >

    <Grid />

</UserControl>

MainWindow.xaml

<Window x:Class="OneWayToSourceTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:oneWayToSourceTest="clr-namespace:OneWayToSourceTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <oneWayToSourceTest:ButterflyControl RefreshAction="{Binding RefreshAction, Mode=OneWayToSource}" />

</Window>

Startup.cs

using System;

namespace OneWayToSourceTest
{
    public static class Startup
    {

        [STAThread]
        public static void Main()
        {
            MainWindow window = new MainWindow();
        }

    }
}

When I run this program, the output window shows:

RefreshAction changed from '' to 'Refresh'
RefreshAction changed from 'Refresh' to ''

Have I made a mistake somewhere, or am I taking the wrong approach? And if I'm taking the wrong approach, what is the appropriate way to initialize a OneWayToSource dependency property with a non-static value?

Upvotes: 0

Views: 145

Answers (1)

Clemens
Clemens

Reputation: 128098

Move the initialization to a Loaded event handler, which is called after the Binding has been established.

public ButterflyControl()
{
    InitializeComponent();

    Loaded += (s, e) => RefreshAction = Refresh;
}

See Object Lifetime Events for further information.

Upvotes: 1

Related Questions