gpt4 book ai didi

c# - 如何访问主窗体公共(public)属性 WPF

转载 作者:行者123 更新时间:2023-11-30 18:44:37 29 4
gpt4 key购买 nike

我有一个用户控件,我必须从那里调用包含用户控件的窗口的属性,我如何才能访问该属性。假设我的窗口中有 Title 属性,并且我想从用户控件访问窗口的 Title 属性。任何想法

可以吗

(App.Current.MainWindow as MainWindow).Title;

提前致谢

最佳答案

此代码将获取用户控件所在的父窗口:

FrameworkElement parent = (FrameworkElement)this.Parent;
while (true)
{
if (parent == null)
break;
if (parent is Page)
{
//Do your stuff here. MessageBox is for demo only
MessageBox.Show(((Window)parent).Title);
break;
}
parent = (FrameworkElement)parent.Parent;
}

关于c# - 如何访问主窗体公共(public)属性 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2542970/

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