在Changed Callback中设置DependencyProperty值

时间:2015-04-24 05:30:43

标签: c# wpf mvvm dependency-properties

我的UserControl中有一个DependencyProperty,它使用布尔来触发行为。在这种情况下,它是一个UserControl,旨在公开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();
    }
}

1 个答案:

答案 0 :(得分:1)

通常情况下,如果您发现自己处于UI线程上执行代码的情况,但在某个有意义的点之前,总是可以通过使用UI线程来卸载您的工作直到将来的某个点# 39; s Dispatcher。

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

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

此方法有一个带有DispatcherPriority枚举的重载。使用适当的优先级。通常情况下,我会使用priority ContextIdle {{1}},在您需要的所有内容完成后执行。