首页 > 解决方案 > 如何初始化 OneWayToSource 依赖属性?

问题描述

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

蝴蝶控件.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.
        }

    }
}

蝴蝶控件.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>

主窗口.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>

启动.cs

using System;

namespace OneWayToSourceTest
{
    public static class Startup
    {

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

    }
}

当我运行这个程序时,输出窗口显示:

RefreshAction 从 '' 更改为 'Refresh'
RefreshAction 从 'Refresh' 更改为 ''

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

标签: c#wpf

解决方案


将初始化移动到 Loaded 事件处理程序,该处理程序在建立绑定后调用。

public ButterflyControl()
{
    InitializeComponent();

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

有关详细信息,请参阅对象生命周期事件


推荐阅读