C# 如何初始化OneWayToSource依赖项属性?

C# 如何初始化OneWayToSource依赖项属性?,c#,wpf,C#,Wpf,我的控件中有一个dependency属性,它被绑定到视图模型的OneWayToSource。我不知道如何用非静态值初始化它。如果我尝试在控件的构造函数中初始化它,则属性值会更改为所需的值,但由于某些原因会立即更改回。考虑这个代码: ButterflyControl.xaml.cs using System; using System.Diagnostics; using System.Windows; namespace OneWayToSourceTest { public part

我的控件中有一个dependency属性,它被绑定到视图模型的OneWayToSource。我不知道如何用非静态值初始化它。如果我尝试在控件的构造函数中初始化它,则属性值会更改为所需的值,但由于某些原因会立即更改回。考虑这个代码:

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

    }
}
using System;

namespace OneWayToSourceTest
{
    public static class Startup
    {

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

    }
}
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>
<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>
运行此程序时,输出窗口显示:

RefreshAction从“”更改为“刷新”
RefreshAction从“刷新”更改为“”


我是否在某个地方犯了错误,或者我采取了错误的方法?如果我采取了错误的方法,那么用非静态值初始化OneWayToSource依赖项属性的合适方法是什么?

将初始化移动到已加载的事件处理程序,该处理程序在绑定建立后调用

public ButterflyControl()
{
    InitializeComponent();

    Loaded += (s, e) => RefreshAction = Refresh;
}
有关更多信息,请参阅