我试图在样式中设置附加属性,我想用它们附加行为。但是我不能让它工作。继承人代码:
附属财产
public class TestBehaviour
{
public static bool GetTest(Grid grid)
{
return (bool)grid.GetValue(TestProperty);
}
public static void SetTest(Grid grid, bool value)
{
grid.SetValue(TestProperty, value);
}
public static readonly DependencyProperty TestProperty = DependencyProperty.RegisterAttached("Test", typeof(bool), typeof(Grid));
}
Xaml
<Window x:Class="AttachedPropertyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:AttachedPropertyTest"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="test:TestBehaviour.Test" Value="true"></Setter>
</Style>
</Grid.Style>
</Grid>
附加属性的所有者类型必须是声明该属性的类,而TestBehaviour
不是Grid
。将声明更改为:
public static readonly DependencyProperty TestProperty =
DependencyProperty.RegisterAttached("Test", typeof(bool), typeof(TestBehaviour));
请参阅MSDN文档中的RegisterAttached:
ownerType-正在注册依赖项属性的所有者类型
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句