【问题标题】:Attached property not working properly on control附加属性在控件上无法正常工作
【发布时间】:2011-10-16 09:54:43
【问题描述】:

当我将一个属性声明为普通依赖属性时,它会起作用,但是当它被声明为附加时,它不会。我不确定,我在这里错过了什么。请帮忙。以下是代码。

(带有依赖属性的第 1 组效果很好,但带有附加依赖属性的第 2 组效果不佳)

<StackPanel Name="PanelControl"  Orientation="{Binding ElementName=MainWindow, Path=ControlOrientation, Converter={StaticResource ResourceKey=LocalConvertor}}"/>

设置 1

FrameworkPropertyMetadata metaData1 = new FrameworkPropertyMetadata(Orientation.Vertical, FrameworkPropertyMetadataOptions.AffectsRender);                     
ControlOrientationProperty = DependencyProperty.RegisterAttached("ControlOrientation", typeof(Orientation), typeof(CustomTextBoxUsingDependencyProperty), metaData1); 

public Orientation ControlOrientation
{
    get { return (Orientation)(GetValue(ControlOrientationProperty)); }
    set { SetValue(ControlOrientationProperty, value); }
}
 <clist:CustomTextBoxUsingDependencyProperty Width="742" Height="100" ControlOrientation="Horizontal"/>

第二组

ControlOrientationProperty = DependencyProperty.RegisterAttached("ControlOrientation", typeof(Orientation), typeof(CustomTextBoxUsingDependencyProperty), metaData1); 

public static void SetControlOrientation(UIElement element, Orientation value)
{
    element.SetValue(CustomTextBoxUsingDependencyProperty.ControlOrientationProperty, value);
}
public static Orientation GetControlOrientation(UIElement element)
{
    return (Orientation)element.GetValue(CustomTextBoxUsingDependencyProperty.ControlOrientationProperty);
}
<clist:CustomTextBoxUsingDependencyProperty Width="742" Height="100">
    <Button Content="Test" clist:CustomTextBoxUsingDependencyProperty.ControlOrientation="Horizontal"/>
</clist:CustomTextBoxUsingDependencyProperty>

【问题讨论】:

  • “正确”是什么意思?您可以使用属性更改回调并根据需要处理属性值。
  • 我的意思是,案例 2 没有更新 StackPanel 的方向,但是当我将属性更改事件附加到附加属性时;我可以看到新的价值即将到来。现在,这只是一次绑定,在情况 1 中,也没有附加属性更改的事件处理程序。那么为什么案例 1 有效而案例 2 无效?我想不通!
  • StackPanel 绑定到 MainWindow,而属性用于自定义控件和按钮。

标签: wpf wpf-controls attached-properties


【解决方案1】:

您应该编写 PropertyChanged 回调来执行操作

ControlOrientationProperty = DependencyProperty.RegisterAttached("ControlOrientation", 
                                typeof(Orientation), 
                                typeof(CustomTextBoxUsingDependencyProperty), 
                                new FrameworkPropertyMetadata(OnControlOrentationChnaged)); 

public static void SetControlOrientation(UIElement element, Orientation value)
{
    element.SetValue(CustomTextBoxUsingDependencyProperty.ControlOrientationProperty, value);
}
public static Orientation GetControlOrientation(UIElement element)
{
    return (Orientation)element.GetValue(CustomTextBoxUsingDependencyProperty.ControlOrientationProperty);
}

private static void OnControlOrentationChnaged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{       
    // o will be the control on which the property is applied
    //Your logic here
}

这可能会有所帮助

【讨论】:

  • 私有静态无效 OnControlOrentationChnaged(DependencyObject o, DependencyPropertyChangedEventArgs e) { o.SetValue(CustomTextBoxUsingDependencyProperty.ControlOrientationProperty, (Or​​ientation)e.NewValue); } 结果还是一样。另外,另一个问题是,为什么这里需要 PropertyChanged 回调(如附加属性的情况下),为什么在正常依赖属性实现的情况下它工作正常?
【解决方案2】:

仍然不确定问题是什么,但我认为这是您的 StackPanel 上的绑定?如果是这样,那么您需要更改Binding.Path,如果您针对附加属性,则需要括号和所属类,即

{Binding (clist:CustomTextBoxUsingDependencyProperty.ControlOrientation),
         ElementName=...}

【讨论】:

  • 绑定是 并定义了资源。正如我之前提到的,当我将该属性用作普通依赖属性时,它工作正常,但是当我将它作为附加属性实现时它不起作用(原始帖子中的完整列表)。
  • 得到了我的答案。刚刚了解到,附加属性不能作为数据绑定的来源。
  • @VinayDwivedi:您从哪里得到不能绑定到附加属性的想法?我已经看到了。
  • @VinayDwivedi:你在说什么?您只需要修改我在答案中显示的路径,当然您可以绑定到附加属性,您只需要在它们周围加上括号并限定它们的位置。 (在绑定中,没有任何前缀的第一个输入(例如ElementName=Path=)将是您不知道的路径)
  • 这里,我也问过这个问题@MSDN,social.msdn.microsoft.com/Forums/en-US/wpf/thread/…
【解决方案3】:

因为 DP 仅用于您声明它的控件。这就是为什么这与 DP 完美配合-

<StackPanel Name="PanelControl"  Orientation="{Binding ElementName=MainWindow, Path=ControlOrientation, Converter={StaticResource ResourceKey=LocalConvertor}}"/>

但是,当您将其声明为附加属性时,它可以用于您想要的任何控件。如果您在加载此控件后看到您的输出窗口,则该绑定可能会失败,因为它无法重新设置 ControlOrientation 的绑定。您需要像这样进行绑定 -

<StackPanel Name="PanelControl"  Orientation="{Binding ElementName=MainWindow, Path=(clist:CustomTextBoxUsingDependencyProperty.ControlOrientation), Converter={StaticResource ResourceKey=LocalConvertor}}"/>

对于你的两个集合,我可以看到两个属性都注册为附加。是错字吗?

【讨论】:

  • 是的,那是一种类型...对不起。这也行不通。请查看我的上一条评论,我已将代码上传到 skydrive。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 2022-01-06
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 2010-11-07
相关资源
最近更新 更多