如何访问主窗体公共属性WPF

时间:2010-03-30 05:05:36

标签: c# wpf

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

没关系

(App.Current.MainWindow as MainWindow)。标题;

提前致谢

2 个答案:

答案 0 :(得分:0)

为什么不将父窗口的title属性与控件的属性绑定?


Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window Title" Height="500" Width="650" ResizeMode="NoResize" x:Name="us1">   
      TextBox Name="txtBlk" Text="{Binding Path=Title, ElementName=us1}"/>          
/Window>

答案 1 :(得分:0)

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

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;
        }
相关问题