繁体   English   中英

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

[英]Attached Property. How to use SetValue in PropertyChangedCallback

我需要附加属性,将焦点设置为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中设置为true ...它在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);
    }
  }
}
}
}

问候马可。

问题在于:

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

您应该使用SetCurrentValue来设置DP。

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

说明

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

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

此方法由组件使用,该组件以编程方式设置其自身属性之一的值,而不禁用应用程序声明的属性使用。 SetCurrentValue方法更改属性的有效值,但现有的触发器,数据绑定和样式将继续工作


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

因此,我们可以做的是通过使用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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM