Question

I'm learning XAML and I'm having a problem binding content of a control to a settings property from a child window.

Here's a quick example I've made to make it more clear. Calling the child from the main window:

Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
    Dim OwndWindow As New WindowChild
    OwndWindow.Owner = Me
    OwndWindow.ShowDialog()
End Sub

And this is the child:

<Window x:Class="WindowChild"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication2"
    Title="WindowChild" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider x:Key="ObjDatPro" ObjectType="{x:Type src:TestSettings}"></ObjectDataProvider>
    </Window.Resources>

    <Grid DataContext="{Binding Source={StaticResource ObjDatPro}}">
        <CheckBox Content="CheckBox" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"
                  IsChecked="{Binding Default.BoolSetting, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

At this point Visual Studio is reporting an error: The name "TestSettings" does not exist in the namespace "clr-namespace:WpfApplication2". (Line 7, Column 49)

I've tried changing this part to WindowChild.TestSettings, but then VS complains that nested types are not supported.

Changing CLR namespace to WpfApplication2.WindowChild or even WindowChild doesn't do the trick, VS says: Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'WindowChild' that could not be found.

What am I doing wrong here?

Was it helpful?

Solution

From the OP:

I've finally figured it out: instead of using <Window.Resources> and ObjectDataProvider all it needed was <Window.DataContext><src:TestSettings/></Window.DataContext>.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top