附属物。如何在PropertyChangedCallback中使用SetValue

时间:2014-02-18 19:39:33

标签: c# wpf attached-properties

我需要附加属性,将焦点设置为ViewModel中的UIElement。 我已经创建了Attached属性,在PropertyChangedCallback中我将焦点设置为UIElement。

private static void VoySetFocusChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
  if (o is UIElement)
  {
    if ((bool)e.NewValue)
    {
      (o as UIElement).Focus();
      (o as UIElement).SetValue(VoySetFocusProperty, false);
    }
  }
}

但是我希望它像霰弹枪中的触发器一样工作。我在ViewModel中设置为Test ...    它在MyAttachedProperties类中调用PropertyChangedCallback,将焦点设置为UIElement

((o as UIElement).Focus();

并且ViewModel中的Test值返回false

((o as UIElement).SetValue(VoySetFocusProperty, false);)

一切似乎都运行良好,但SetValue不会在ViewModel中改变我的值。

完整代码:

查看:

<Window x:Class="WpfAttachedProperty.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfAttachedProperty"
    Title="MainWindow" Height="127" Width="316">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <TextBox local:MyAttachedProperties.VoySetFocus="{Binding Path=Test,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Text="Focus Me"/>
    <Button Grid.Row="1" Content="Click to Focus" HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Width="75" Command="{Binding MyCommand}" />
</Grid>

代码背后:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfAttachedProperty
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow:Window
{
  public MainWindow()
  {
    InitializeComponent();
    DataContext = new ViewModel();
  }
}

/// <summary>
/// Command Class
/// </summary>
public class DelegateCommand:ICommand
{
  private readonly Action _action;

  public DelegateCommand(Action action)
  {
    _action = action;
  }

  public void Execute(object parameter)
  {
    _action();
  }

  public bool CanExecute(object parameter)
  {
    return true;
  }

  public event EventHandler CanExecuteChanged
  {
    add
    {
    }
    remove
    {
    }
  }
}

/// <summary>
/// ViewModelClass
/// </summary>
public class ViewModel:INotifyPropertyChanged
{
  private bool _test = false;
  public bool Test
  {
    get
    {
      return _test;
    }
    set
    {
      _test = value;
      this.NotifyPropertyChanged("Test");
    }
  }
  public ICommand MyCommand
  {
    get
    {
      return new DelegateCommand(SetTestToTrue);
    }
  }
  private void SetTestToTrue()
  {
    this.Test = true;
  }

  #region INotifyPropertyChanged
  public void NotifyPropertyChanged(String PropertyName)
  {
    if (this.PropertyChanged != null)
    {
      this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }
  }
  public event PropertyChangedEventHandler PropertyChanged;
  #endregion
}


public class MyAttachedProperties
{
  public static Object GetVoySetFocus(DependencyObject obj)
  {
    return (Object)obj.GetValue(VoySetFocusProperty);
  }

public static void SetVoySetFocus(DependencyObject obj, Object value)
{
  obj.SetValue(VoySetFocusProperty, value);
}

public static readonly DependencyProperty VoySetFocusProperty =
    DependencyProperty.RegisterAttached("VoySetFocus", typeof(bool), typeof(MyAttachedProperties), new UIPropertyMetadata(false, new PropertyChangedCallback(VoySetFocusChanged), new CoerceValueCallback(CoerceVoySetFocus)));

private static object CoerceVoySetFocus(DependencyObject d, object baseValue)
{
  return (bool)baseValue;
}

private static void VoySetFocusChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
  if (o is UIElement)
  {
    if ((bool)e.NewValue)
    {
      (o as UIElement).Focus();
      // Doesn't set Test to false in ViewModel
      (o as UIElement).SetValue(VoySetFocusProperty, false);
    }
  }
}
}
}

Greetings Marko。

1 个答案:

答案 0 :(得分:5)

问题在于:

((o as UIElement).SetValue(VoySetFocusProperty, false);)

您应该使用 SetCurrentValue 来设置DP。

((o as UIElement).SetCurrentValue(VoySetFocusProperty, false);)

<强>解释

直接设置任何DependencyProperty的值会破坏它与source属性的绑定。

但是,SetCurrentValue不会破坏绑定并将值推回源属性。 MSDN对SetCurrentValue的解释:

  

此方法由以编程方式设置的组件使用   在不禁用应用程序的情况下,其自身属性之一的值   宣布使用该财产。 SetCurrentValue方法更改   有效的属性值,但现有的触发器,数据绑定,   和样式将继续有效


此外,我认为在PropertyChanged回调中设置它不会将其传播回Viewmodel,如果该操作是同步操作的,因为它仅从Viewmodel属性设置器进行回调。

因此,我们可以做的是异步执行此操作,方法是使用BeginInvoke将其排入调度程序,以便它传播到viewmodel Test属性。

(o as UIElement).Focus();
Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate
{
    // Doesn't set Test to false in ViewModel
    (o as UIElement).SetCurrentValue(VoySetFocusProperty, false);
});