gpt4 book ai didi

c# - 在更改的回调中设置 DependencyProperty 值

转载 作者:行者123 更新时间:2023-12-03 10:35:51 31 4
gpt4 key购买 nike

我的 UserControl 中有一个 DependencyProperty,它使用 bool 值来触发行为。在这种情况下,它是一个用户控件,旨在公开 SaveFileDialog 的功能,并且它使用 DialogVisible DependencyProperty。当属性设置为 true 时,我在属性更改的回调中调用 SaveFileDialog 上的 ShowDialog 方法,然后尝试将 DialogVisible 属性设置回 false,但这不会传播回绑定(bind)。我希望这是因为我在回调中设置了值。有没有办法解决?

提供的代码:

using System.Windows;

/// <summary>
/// Interaction logic for SaveFileDialog.xaml.
/// NOTE: Bindings for DialogVisible and FileName must
/// use TwoWay mode or dialog will not function as desired.
/// </summary>
public partial class SaveFileDialog
{
#region Depedancy Properties

public static readonly DependencyProperty FilterProperty = DependencyProperty.RegisterAttached(
"Filter",
typeof(string),
typeof(SaveFileDialog),
new PropertyMetadata(FilterProperty_Changed));

public static readonly DependencyProperty FileNameProperty = DependencyProperty.RegisterAttached(
"FileName",
typeof(string),
typeof(SaveFileDialog),
new PropertyMetadata(FileNameProperty_Changed));

public static readonly DependencyProperty InitialDirectoryProperty = DependencyProperty.RegisterAttached(
"InitialDirectory",
typeof(string),
typeof(SaveFileDialog),
new PropertyMetadata(InitialDirectoryProperty_Changed));

public static readonly DependencyProperty DefaultExtensionProperty = DependencyProperty.RegisterAttached(
"DefaultExtension",
typeof(string),
typeof(SaveFileDialog),
new PropertyMetadata(DefaultExtensionProperty_Changed));

public static readonly DependencyProperty DialogVisibleProperty = DependencyProperty.RegisterAttached(
"DialogVisible",
typeof(bool),
typeof(SaveFileDialog),
new PropertyMetadata(DialogVisibleProperty_Changed));

public string Filter
{
get { return (string)this.GetValue(FilterProperty); }
set { this.SetValue(FilterProperty, value); }
}

public string FileName
{
get { return (string)this.GetValue(FileNameProperty); }
set { this.SetValue(FileNameProperty, value); }
}

public string InitialDirectory
{
get { return (string)this.GetValue(InitialDirectoryProperty); }
set { this.SetValue(InitialDirectoryProperty, value); }
}

public string DefaultExtension
{
get { return (string)this.GetValue(DefaultExtensionProperty); }
set { this.SetValue(DefaultExtensionProperty, value); }
}

public bool DialogVisible
{
get { return (bool)this.GetValue(DialogVisibleProperty); }
set { this.SetValue(DialogVisibleProperty, value); }
}

private static void FilterProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (SaveFileDialog)d;
control._dialog.Filter = (string)e.NewValue;
}

private static void FileNameProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (SaveFileDialog)d;
control._dialog.FileName = (string)e.NewValue;
}

private static void InitialDirectoryProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (SaveFileDialog)d;
control._dialog.InitialDirectory = (string)e.NewValue;
}

private static void DefaultExtensionProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (SaveFileDialog)d;
control._dialog.DefaultExt = (string)e.NewValue;
}

private static void DialogVisibleProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (SaveFileDialog)d;

if ((bool)e.NewValue)
{
control._dialog.ShowDialog();
control.DialogVisible = false;
control.FileName = control._dialog.FileName;
}
}

#endregion

private readonly Microsoft.Win32.SaveFileDialog _dialog = new Microsoft.Win32.SaveFileDialog();

public SaveFileDialog()
{
InitializeComponent();
}
}

最佳答案

通常,如果您发现自己的代码在 UI 线程上执行,但在它有意义之前,总是可以通过使用 UI 线程的 Dispatcher 将您的工作卸载到将来某个时间点。

例如,如果您有代码在启动时运行,但您希望 UI 元素已加载并准备好进行交互,您可以稍后使用 Dispatcher 执行您的 UI 代码。

为此,首先您必须获取 UI Dispatcher。您可以通过 Application.Current.Dispatcher 进行此操作并通过 BeginInvoke 卸载更新的执行,传入您希望稍后调用的方法或 lambda。

此方法有一个重载,它采用 DispatcherPriority枚举。使用适当的优先级。通常,我使用 priorityContextIdle ,它在您需要的几乎所有内容完成后执行。

关于c# - 在更改的回调中设置 DependencyProperty 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29839426/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com