从子选项卡访问主窗口属性
我希望能够从 TabControl 页面内访问窗口的公共成员。为此,我尝试从使用 XAML: 切换
<TabControl TabStripPlacement="Left" HorizontalContentAlignment="Left" Name="wizardTabs">
<TabItem Header="Login">
<Frame Source="LoginPage.xaml" IsTabStop="False"/>
</TabItem>
</TabControl>
到基于代码的选项卡填充,在主窗口的 Loaded 事件期间:
newFrame = new Frame();
newFrame.Source = new Uri(@"\LoginPage.xaml", UriKind.Relative);
newFrame.IsTabStop = false;
tabItem = new TabItem();
tabItem.Header = "Login";
tabItem.Content = newFrame;
wizardTabs.Items.Add(tabItem);
我在选项卡中使用 Frame 并加载框架中的 XAML 页面。通过这种方式,我认为构造函数可供我使用,以便我可以将 this 指针传递到 Page 类中:
LoginPage loginPage = newFrame.Content as LoginPage;
loginPage.parent = this;
但我发现加载 XAML 需要一个空构造函数。另外,newFrame.Content 始终为 NULL。我认为这与现阶段尚未加载 XAML 有关,但我无法弄清楚何时执行 XAML 加载,如果稍后执行,我如何设置“父”指针。
感谢您的任何建议。我是 WPF 编码新手,请指导我。
I want to be able to access public members of my Window from within its TabControl Pages. To do that, I've tried switched from using XAML:
<TabControl TabStripPlacement="Left" HorizontalContentAlignment="Left" Name="wizardTabs">
<TabItem Header="Login">
<Frame Source="LoginPage.xaml" IsTabStop="False"/>
</TabItem>
</TabControl>
to code-based population of the tabs instead, during the Loaded event of the main window:
newFrame = new Frame();
newFrame.Source = new Uri(@"\LoginPage.xaml", UriKind.Relative);
newFrame.IsTabStop = false;
tabItem = new TabItem();
tabItem.Header = "Login";
tabItem.Content = newFrame;
wizardTabs.Items.Add(tabItem);
I'm using a Frame in the Tab and loading a XAML Page in the Frame. In this way, I thought that the constructor would be available to me so that I can pass a this pointer into the Page class:
LoginPage loginPage = newFrame.Content as LoginPage;
loginPage.parent = this;
but I have discovered that loading XAML requires an empty constructor. Also, newFrame.Content is always NULL. I'm thinking it's related to the fact that the XAML hasn't been loaded at this stage, but I can't figure out when that XAML load will be performed, and if it's later, how I can set the "parent" pointer.
Thanks for any suggestions. I'm a newbie WPF coder, so please guide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个答案。您可以使用 Setter Property 将框架绑定到 content 属性:
Take a look at this answer. You can use Setter Property to bind your frame to the content property:
好吧,我找到了解决方案。我可以直接使用对象实例而不是 URI 使用 Navigate() 方法,而不是使用 .Source 属性。所以代码看起来像这样:
Ok I found a solution. Instead of using the .Source property, I can use the Navigate() method using an instance of the object directly instead of using a URI. So the code looks like this instead: