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

标签 c# wpf

我有一个用户控件,我必须从那里调用包含用户控件的窗口的属性,我如何才能访问该属性。 假设我的窗口中有 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/

相关文章:

c# - 如何处理 WPF 数据绑定(bind)菜单中的单击事件

c# - 垂直合并 WPF DataGrid 中的单元格

c# - Visual Studio 2015 Xamarin.Android - 将图像库中的 throw 限制为每次 throw 仅一项

c# - .NET ASP.NET Web 应用程序 - 持续集成解决方案

c# - 如何验证矩形是否完全包含在区域或图形路径中

c# - 实现不必要的接口(interface)成员或创建新接口(interface)?

WPF 图标规范以支持 Windows 7 并向后兼容

c# - 在父 View 模型中公开 subview 模型的属性

c# - Azure Function Apps - 使用登录用户的凭据来读取/更新 Azure 资源

c# - 仅在 Win8 下 Storyboard中的内存泄漏?