附加属性在控件上无法正常工作
当我将属性声明为普通依赖属性时,它会起作用,但是当我将其声明为附加属性时,它不会起作用。我不确定,我在这里缺少什么。请帮忙。以下是代码。
(具有依赖属性的集 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"/>
集 2
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>
When I declare a property as normal dependency property then it works however when declared it as attached, it doesn't. I am not sure, what am I missing here. Please help. Following is the code.
(Set 1 with dependency property works good but Set 2 with attached dependency property doesn't)
<StackPanel Name="PanelControl" Orientation="{Binding ElementName=MainWindow, Path=ControlOrientation, Converter={StaticResource ResourceKey=LocalConvertor}}"/>
Set 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"/>
Set 2
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该编写 PropertyChanged CallBack 来执行操作
这可能会有所帮助
You should write the PropertyChanged CallBack to perform an action
This might help
仍然不确定问题是什么,但我认为它是您的 StackPanel 上的绑定?如果是这样,那么您需要更改
Binding.Path
,如果您定位附加属性,则需要括号和所属类,即Still not sure what the problem is but i would presume that it is the binding on your StackPanel? If so then you need to change the
Binding.Path
, if you target an attached property you need parentheses and the owning class, i.e.由于 DP 仅用于声明它的控件。这就是为什么它可以与 DP 完美配合 -
但是,当您将其声明为附加属性时,它可以用于您想要的任何控件。如果加载此控件后看到输出窗口,则绑定可能会失败,因为它无法重新绑定 ControlOrientation。您需要像这样进行绑定 -
同样对于您的两个集合,我可以看到两个属性都注册为附加。是打字错误吗?
Since DP's are meant to used only for the control on which you declared it. That's why this is working perfectly working fine with DP-
However, when you declare it as an attached property it can be used for any control you want. If you see your output window after loading of this control that binding might be failing since it wasn't able to reslove binding for ControlOrientation. You need to do binding like this -
Also for both of your sets, i can see both properties are Register as Attached. Is it typo?