在更改的回调中设置 DependencyProperty 值
Set DependencyProperty Value in Changed Callback
我的用户控件中有一个依赖项属性,它使用布尔值来触发行为。在这种情况下,它是一个 UserControl,旨在公开 SaveFileDialog 的功能,并且它使用 DialogVisible Dependency属性。当 属性 设置为 true 时,我在 属性 更改的回调中调用 SaveFileDialog 上的 ShowDialog 方法,然后我尝试将 DialogVisible 属性 设置回 false,但这确实不会传播回绑定。我希望这是因为我在回调中设置了值。有什么解决办法吗?
提供的代码:
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 调度程序。您可以通过 Application.Current.Dispatcher
执行此操作并通过 BeginInvoke
卸载更新的执行,传入您希望稍后调用的方法或 lambda。
此方法有一个采用 DispatcherPriority
枚举的重载。使用适当的优先级。通常,我使用 ContextIdle
的 priority,它会在您需要的几乎所有内容都已完成后执行。
我的用户控件中有一个依赖项属性,它使用布尔值来触发行为。在这种情况下,它是一个 UserControl,旨在公开 SaveFileDialog 的功能,并且它使用 DialogVisible Dependency属性。当 属性 设置为 true 时,我在 属性 更改的回调中调用 SaveFileDialog 上的 ShowDialog 方法,然后我尝试将 DialogVisible 属性 设置回 false,但这确实不会传播回绑定。我希望这是因为我在回调中设置了值。有什么解决办法吗?
提供的代码:
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 调度程序。您可以通过 Application.Current.Dispatcher
执行此操作并通过 BeginInvoke
卸载更新的执行,传入您希望稍后调用的方法或 lambda。
此方法有一个采用 DispatcherPriority
枚举的重载。使用适当的优先级。通常,我使用 ContextIdle
的 priority,它会在您需要的几乎所有内容都已完成后执行。