附加属性在控件上无法正常工作

发布于 2024-12-10 04:09:48 字数 1869 浏览 0 评论 0原文

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

(具有依赖属性的集 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以歌曲疗慰 2024-12-17 04:09:48

您应该编写 PropertyChanged CallBack 来执行操作

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
}

这可能会有所帮助

You should write the PropertyChanged CallBack to perform an action

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
}

This might help

江湖彼岸 2024-12-17 04:09:48

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

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

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.

{Binding (clist:CustomTextBoxUsingDependencyProperty.ControlOrientation),
         ElementName=...}
奢欲 2024-12-17 04:09:48

由于 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}}"/>

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

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-

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

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 -

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

Also for both of your sets, i can see both properties are Register as Attached. Is it typo?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文