c# - 如何初始化 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 依赖属性的适当方法是什么?
解决方案
将初始化移动到 Loaded 事件处理程序,该处理程序在建立绑定后调用。
public ButterflyControl()
{
InitializeComponent();
Loaded += (s, e) => RefreshAction = Refresh;
}
有关详细信息,请参阅对象生命周期事件。
推荐阅读
- arrays - 在 Matlab 中用一个三阶微分方程求解微分方程组时出错。数组的尺寸不一致
- instagram - 图形 API 的 Instagram 访问令牌
- python - 如何有效地计算 pandas 中元素的共存度
- java - 考虑到它的创建顺序,我如何管理片段和活动?
- linux - 当以该用户身份运行程序时,如何将环境重置为该用户的默认值?
- javascript - 如何对这个对象数组进行排序,因为它在 JS 中有 x 和 y 坐标?
- apache - (111)连接被拒绝 - Apache 反向代理和 Tomcat 8.5.51 - Docker Compose
- c# - 如何使用包含过滤多个值
- mysql - MySQL STR_TO_DATE 总是返回 2019
- javascript - 根据数字将 message.author.id 放入多个数组中?