附属财产在控制方面不能正常运作

时间:2011-10-16 09:54:43

标签: wpf wpf-controls attached-properties

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

(依赖属性设置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>

3 个答案:

答案 0 :(得分:0)

您应该编写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
}

这可能会有所帮助

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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