mjordan
mjordan

Reputation: 369

WPF Window/Frame/Page

I have a Window which holds a Grid containing: a Label and a Frame. The Frame holds a Page. The Page has a Button and a Label.

Both Labels (on Window and Page) are bound to the same string property, which initially works correctly.

The Button (on the page) changes the string property, which I expect to change both the Label on the Window and the Label on the Page.

The problem is that it only changes the Label on the Page and not the Label on the Window. Is there a way to have a button on a page change an element in it's parent window? Also, if there is an explanation of why this is happening I would appreciate it.

ViewModel with Output Screen Shots

Window 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>

Page 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>

Upvotes: 0

Views: 1222

Answers (1)

Dipen Shah
Dipen Shah

Reputation: 26085

You have two different objects used for DataContext of window and page, make sure you are using same object.

<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>

Upvotes: 1

Related Questions