简体   繁体   中英

WPF Nested UserControl Datacontext

I have 2 classes (InnerModel and OuterModel). OuterModel contains 2 InnerModel instances. I would like to create UserControls for them (InnerUserControl and OuterUserControl). OuterUserControl contains 2 InnerUserControls. But I can't figure out how to make binding works in this case. Below is the complete code of what I try to do. Please advise how to fix it to get the same result as on a pic at the end. Thanks in advance!

MainWindow.xaml.cs

<Window x:Class="NestedUserControl.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:NestedUserControl"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="450">
<Grid>
    <local:OuterUserControl x:Name="test"/>
</Grid>

MainWindow.xaml

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var model = new OuterModel("TEST1", "TEST2");
        test.DataContext = model;
    }
}

InnerModel.cs

public class InnerModel : INotifyPropertyChanged
{
    public String Data
    {
        get { return data; }
        set { data = value; }
    }
    private string data;

    public event PropertyChangedEventHandler PropertyChanged;

    public InnerModel(string _data) => data = _data;
    public void OnPropertyChanged([CallerMemberName]string prop = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}

InnerUserControl.xaml

<UserControl x:Class="NestedUserControl.InnerUserControl"
         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" 
         xmlns:local="clr-namespace:NestedUserControl"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="450">
<Grid>
    <TextBlock Text="{Binding Path=Data}"/>
</Grid>

InnerUserControl.xaml.cs

public partial class InnerUserControl : UserControl
{
    public InnerUserControl()
    {
        InitializeComponent();
    }
}

OuterModel.cs

public class OuterModel : INotifyPropertyChanged
{
    public InnerModel model1;
    public InnerModel model2;

    public event PropertyChangedEventHandler PropertyChanged;

    public OuterModel(string data1, string data2)
    {
        model1 = new InnerModel(data1);
        model2 = new InnerModel(data2);
    }
    public void OnPropertyChanged([CallerMemberName]string prop = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}

OuterUserControl.xaml

<UserControl x:Class="NestedUserControl.OuterUserControl"
         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" 
         xmlns:local="clr-namespace:NestedUserControl"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="450">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <local:InnerUserControl Grid.Row="0" x:Name="inner1" DataContext="model1"/>
    <local:InnerUserControl Grid.Row="1" x:Name="inner2" DataContext="model2"/>
</Grid>

OuerUserControl.xaml.cs

public partial class OuterUserControl : UserControl
{
    public OuterUserControl()
    {
        InitializeComponent();
    }
}

Working binding MainWindow.xaml.cs

Welcome to SO!

Two problems here, first of all your inner models need to be properties, so change their declarations to this:

public InnerModel model1 {get; set;}
public InnerModel model2 {get; set;}

Second problem is with your bindings, you need to do this instead:

<local:InnerUserControl Grid.Row="0" x:Name="inner1" DataContext="{Binding model1}"/>
<local:InnerUserControl Grid.Row="1" x:Name="inner2" DataContext="{Binding model2}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM