我正在尝试在样式中设置附加属性,我希望使用它们来附加行为。然而,我无法让它起作用。 以下是代码:
附加属性
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>
(test:TestBehaviour.Test)
? - Pragmateek