gpt4 book ai didi

wpf - 在样式 setter 中设置自定义附加属性

转载 作者:行者123 更新时间:2023-12-04 12:52:20 28 4
gpt4 key购买 nike

我试图在样式中设置附加属性,我想用它们来附加行为。但是我无法让它工作。继承人代码:

附加属性

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));

请参阅 RegisterAttached 的 MSDN 文档:

ownerType - The owner type that is registering the dependency property

关于wpf - 在样式 setter 中设置自定义附加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25595272/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com