【发布时间】:2015-04-24 05:30:43
【问题描述】:
我的 UserControl 中有一个 DependencyProperty,它使用布尔值来触发行为。在这种情况下,它是一个用户控件,旨在公开 SaveFileDialog 的功能,并且它使用 DialogVisible DependencyProperty。当属性设置为 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();
}
}
【问题讨论】:
-
为什么要显示所有代码?您的问题是关于 DialogVisible 属性和 DialogVisibleProperty_Changed 回调。其他部分是否相关?但是,如果您能展示如何绑定 DialogVisible 属性,这将有所帮助。
-
不确定原因,但您可以将值设置到 Dispatcher 上,优先级低于 Binding,例如 Background 或 ContextIdle。
-
@Will 效果很好!谢谢。将您的评论转换为答案,以便我接受。
标签: c# wpf mvvm dependency-properties