gpt4 book ai didi

c# - 从静态上下文更改非静态对象

转载 作者:行者123 更新时间:2023-11-30 19:33:14 25 4
gpt4 key购买 nike

我有下一个依赖属性:

public static DependencyProperty IsInReadModeProperty =
DependencyProperty.Register("IsInReadMode", typeof(bool),
typeof(RegCardSearchForm), new PropertyMetadata(false, ReadModeChanged));

我有下一个处理属性更改事件的方法:

public static void ReadModeChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
btnSearch.Visibility = Visibility.Collapsed;
btnExport.Visibility = Visibility.Collapsed;
cbExportWay.Visibility = Visibility.Collapsed;
}
}

但编译器给我的错误是我无法在静态上下文中访问非静态按钮(btnSearch 和 e.t.c)。

我想在属性值改变时改变按钮的可见性。我该如何解决这种情况?

最佳答案

由于(非附加的)DependencyProperties 仅限于在其所有者类型上设置,您只需创建一个实例方法来保存您的逻辑并通过转换 DependencyObject 从静态方法调用它:

public static readonly DependencyProperty IsInReadModeProperty = DependencyProperty.Register(
"IsInReadMode",
typeof(bool),
typeof(RegCardSearchForm),
new UIPropertyMetadata(false, ReadModeChanged));

private static void ReadModeChanged(DependencyObject dObj, DependencyPropertyChangedEventArgs e)
{
RegCardSearchForm form = dObj as RegCardSearchForm;
if (form != null)
form.ReadModeChanged((bool)e.OldValue, (bool)e.NewValue);
}

protected virtual void ReadModeChanged(bool oldValue, bool newValue)
{
// TODO: Add your instance logic.
}

关于c# - 从静态上下文更改非静态对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4040735/

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