【问题标题】:How to click a button of the page to change the source of the frame of Windows?如何点击页面的一个按钮来改变Windows框架的来源?
【发布时间】:2015-05-31 14:55:36
【问题描述】:

我有一个 WPF 项目,我在 Windows 中添加了一个框架,框架的来源是页面。我想实现点击页面中的一个按钮来改变框架的页面。

<Window x:Class="MagicArm.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MagicArm"
    Title="MainWindow">
   <Frame Name="FrameContent"Source="PageStart.xaml"></Frame>
</Window>

PageStart:

<Page x:Class="MagicArm.PageStart"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  Height="452" Width="800"
  Title="PageStart">
<Canvas>
 <button name=""> </button>
</Canvas>

【问题讨论】:

  • 你试过什么?你被困在哪里了?能具体一点吗?
  • 我在页面中有一个按钮,我想更改框架的来源当我点击按钮时,我该怎么办?

标签: wpf frame


【解决方案1】:

编辑:功能性解决方案可能如下所示:

主窗口 XAML:

<StackPanel>
    <Frame Name="frmMainContent" Height="260"
         DataContext="MyPageInformation"
         Source="{Binding}"
         NavigationUIVisibility="Hidden">           
    </Frame>

</StackPanel>

主窗口cs:

 frmMainContent.Source = new Uri("test1.xaml", UriKind.Relative); // initialize frame with the "test1" view

test1 XAML:

<Grid>
    <Button Click="ButtonBase_OnClick" Background="Red" Height="30" Width="100">Go to page 2</Button>
</Grid>

test1 cs:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        NavigationService ns = NavigationService.GetNavigationService(this);
        ns.Navigate(new Uri("test2.xaml", UriKind.Relative));
    }

test2 XAML:

  <Grid>
        <Button Click="ButtonBase_OnClick" Width="100" Height="30" Background="RoyalBlue"> Go to page 1</Button>
    </Grid>

test2 cs:

 NavigationService ns = NavigationService.GetNavigationService(this);
            ns.Navigate(new Uri("test1.xaml", UriKind.Relative));

这是一个使用 NavigationService 的有效解决方案。

【讨论】:

  • 按钮在页面中,页面是框架的来源,不在窗口中。窗口是页面的父窗体。
  • 在这种情况下,我认为您需要 EventPublisher 来通知父视图,请参考:social.technet.microsoft.com/wiki/contents/articles/…
  • 谢谢。这是一个很好的解决方案。如果我想点击页面的按钮关闭窗口,我该怎么做?
  • 请创建另一个包含更多详细信息的问题。您希望框架内的关闭按钮起作用吗?还是?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-16
  • 2021-10-02
  • 2021-11-30
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多