【问题标题】:Set DependencyProperty Value in Changed Callback在更改的回调中设置 DependencyProperty 值
【发布时间】: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


【解决方案1】:

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

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

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

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

【讨论】:

  • 我使用了背景,效果也一样。再次感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2011-01-04
  • 2016-07-21
  • 2014-09-27
  • 1970-01-01
  • 2019-01-16
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多