0

I have two pages TunerPage.xaml and FormCameraSetting.xaml, and FormCameraSetting in TunerPage as Frame through binding, like this:

TunerPage.xaml

<Grid>
    <Grid.ColumnDefinitions>
         <ColumnDefinition Width="300"/>
         <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <UniformGrid Grid.Column="0" Rows="1" Columns="2">
        <Label Content="Shutter" FontFamily="Arial" Foreground="White" VerticalContentAlignment="Center"/>
        <TextBox Style="{StaticResource TextBoxStyle1}" Text="{Binding Shutter, Mode=OneWay}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" IsEnabled ="False"/>
    </UniformGrid>
    <Frame Grid.Column="1" Content="{Binding PageDisplay}" NavigationUIVisibility="Hidden" Loaded="Frame_Loaded" Unloaded="Frame_Unloaded"/>
</Grid>

FormCameraSetting.xaml

<Grid>
    <Grid.ColumnDefinitions>
         <ColumnDefinition Width="300"/>
         <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Label Content="Shutter" FontFamily="Arial" Foreground="White" VerticalContentAlignment="Center"/>
    <TextBox HorizontalContentAlignment="Right" Style="{StaticResource TextBoxStyleForm2}" Text="{Binding Shutter, UpdateSourceTrigger=Explicit}"/>
 </Grid>

Both of them have TextBox. One in TunerPage.xaml is binding Shutter on TunerAction.cs and mode is Oneway. Another in FormCameraSetting.xaml is binding Shutter on CameraSettingViewModel.cs and mode is Twoway.

TunerAction.cs (ViewModel)

public double Shutter
{
    get 
    {
         return 2000; 
    }
}

CameraSettingViewModel.cs (ViewModel)

private double shutter;
public double Shutter
{
    get 
    {
        return shutter;
    }
    set
    {
        shutter = value;
    }
}

Now in xaml.cs, I set their DataContext equal to their ViewModel when the page loaded.

TunerPage.xaml.cs

public TunerAction tunerAction = new TunerAction();
public TunerPage()
{
    InitializeComponent();            
}
private void SettingPageBase_Loaded(object sender, RoutedEventArgs e)
{
    this.DataContext = tunerAction;
}

CameraSettingViewModel.xaml.cs

public CameraSettingViewModel viewModel = new CameraSettingViewModel();
public FormCameraSetting()
{
    InitializeComponent();
}
private void CameraSettingTemplate_Loaded(object sender, RoutedEventArgs e)
{
    this.DataContext = viewModel;
}

When I run VisualStudio, it shows an error:

enter image description here

But, if I change "Shutter" to another name in TunerAction.cs, or set DataContext after InitializeComponent() not when the page loaded, it will not cause this error.

Why did this error happen?

Thanks!

Ricky Chen
  • 55
  • 6
  • It is unclear where exactly the error occurs, and what the DataContext during InitializeComponent is. It might have been set by value inheritance of the DataContext property. – Clemens Nov 15 '22 at 06:38
  • Thanks, I try to realize the load sequence between the page and the frame, but still not find something – Ricky Chen Nov 15 '22 at 06:52
  • The problem can not be diagnosed from what you are showing here. There might be a wrong DataContext assignment in the FormCameraSetting's XAML. – Clemens Nov 15 '22 at 10:55
  • OK, I will continue trying it, thanks – Ricky Chen Nov 17 '22 at 00:46

0 Answers0