【问题标题】:WPF Window/Frame/PageWPF 窗口/框架/页面
【发布时间】:2018-08-17 19:43:16
【问题描述】:

我有一个窗口,其中包含一个网格,其中包含:标签和框架。框架包含一个页面。该页面有一个按钮和一个标签。

两个标签(在窗口和页面上)都绑定到相同的字符串属性,最初可以正常工作。

按钮(在页面上)改变了字符串属性,我希望改变窗口上的标签和页面上的标签。

问题是它只改变了页面上的标签而不是窗口上的标签。有没有办法让页面上的按钮更改其父窗口中的元素?另外,如果能解释为什么会发生这种情况,我将不胜感激。

窗口 Xaml:

<Window.DataContext>
        <ViewModel:MainWindowViewModel/>
</Window.DataContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <Label Content="{Binding SourceTitleHeader, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
               HorizontalAlignment="Left"
               Foreground="Red">
        </Label>
    </Grid>

    <Frame Grid.Row="1" Grid.Column="0" Source="\Views\Page1.xaml">

    </Frame>

</Grid>

页面 Xaml:

<Page.DataContext>
    <ViewModel:MainWindowViewModel/>
</Page.DataContext>

<StackPanel Margin="10">
    <Label Content="{Binding SourceTitleHeader, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
           HorizontalAlignment="Left"
           Margin="0 0 0 20">

    </Label>

    <Button Content="ChangeLabel" Width="100" Height="30" HorizontalAlignment="Left"
            Command="{Binding Refresh_Screen_Command}">

    </Button>
</StackPanel>

【问题讨论】:

  • 你能把你设置DataContext的代码贴出来吗?
  • 似乎窗口和框架都绑定到两个不同的对象,而不是同一个。
  • 感谢您查看此内容。我为 Window 和 Page 添加了我的 Xaml DataContext。如果我理解正确,我相信它们绑定到同一个对象,因为它们最初都持有“Hello World”。
  • 并非如此,它们绑定到两个不同的对象,正如您从代码中看到的那样,wpf 将创建 2 个不同的 MainWindowViewModel 对象;一个用于窗口,另一个用于页面。
  • 哦。这就说得通了。如何克服这一点?我尝试从页面中删除 DataContext,但没有成功。

标签: wpf xaml window


【解决方案1】:

您有两个不同的对象用于窗口和页面的DataContext,请确保您使用的是相同的对象。

<Window.Resources>
    <ResourceDictionary>
        <local:MainWindowViewModel x:Key="ViewModel" />
    </ResourceDictionary>
</Window.Resources>
<Grid DataContext="{StaticResource ViewModel}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <Label Content="{Binding SourceTitleHeader, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
           HorizontalAlignment="Left"
           Foreground="Red">
        </Label>
    </Grid>

    <Frame Grid.Row="1" Grid.Column="0">
        <Frame.Content>
            <local:Page1 DataContext="{StaticResource ViewModel}" />
        </Frame.Content>
    </Frame>

</Grid>

【讨论】:

  • 行得通!谢谢。我不得不将两个“本地:”更改为“视图模型:”和“视图:”,这是我的视图模型和视图所在的位置。我会对此进行研究,但是否容易解释添加 ResourceDictionary 是如何解决问题的?
  • 是的,所以 wpf 中的 Frame 基本上是框架内外控件之间的边界。通常,子控件可以访问来自父级的DataContext 的属性,而Frame 下的控件则不然。将模型添加为静态资源允许您在窗口中访问它,这就是为什么在我发布的代码中,我能够成功地将窗口和框架的 DataContext 设置为同一个对象。
  • 太棒了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-12-09
  • 2013-03-02
  • 2011-07-11
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
相关资源
最近更新 更多