我尝试使用绑定与附加属性。但不能得到它的工作。WPF附加属性数据绑定
public class Attached
{
public static DependencyProperty TestProperty =
DependencyProperty.RegisterAttached("TestProperty", typeof(bool), typeof(Attached),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Inherits));
public static bool GetTest(DependencyObject obj)
{
return (bool)obj.GetValue(TestProperty);
}
public static void SetTest(DependencyObject obj, bool value)
{
obj.SetValue(TestProperty, value);
}
}
的XAML代码:
<Window ...>
<StackPanel local:Attached.Test="true" x:Name="f">
<CheckBox local:Attached.Test="true" IsChecked="{Binding (local:Attached.Test), Mode=TwoWay, RelativeSource={RelativeSource Self}}" />
<CheckBox local:Attached.Test="true" IsChecked="{Binding (local:Attached.Test), Mode=TwoWay}" />
</StackPanel>
</Window>
并且绑定错误:
System.Windows.Data Error: 40 : BindingExpression path error: '(local:Attached.Test)' property not found on 'object' ''StackPanel' (Name='f')'. BindingExpression:Path=(local:Attached.Test); DataItem='StackPanel' (Name='f'); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1')
我已经试过这个,并得到一个例外:Der Eigenschaftspfad istungültig。 “附件”besitzt keineöffentlicheEigenschaft mit dem Namen \“Test \”。 - > Engl:属性无效。 “附加”不拥有公共属性“测试” – SACO 2011-04-29 13:07:16
“RegisterAttached”调用应通过“Test”,而不是“TestProperty”作为属性名称。 – 2011-04-29 15:22:09
...对...好的,谢谢。现在一切正常。 – SACO 2011-05-02 08:36:36