开发者

Attached property not working properly on control

开发者 https://www.devze.com 2023-04-13 02:57 出处:网络
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.

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 Framewor开发者_如何学运维kPropertyMetadata(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>


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


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=...}


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消