簡體   English   中英

WPF 嵌套用戶控件數據上下文

[英]WPF Nested UserControl Datacontext

我有 2 個類(InnerModel 和 OuterModel)。 OuterModel 包含 2 個 InnerModel 實例。 我想為他們創建用戶控件(InnerUserControl 和 OuterUserControl)。 OuterUserControl 包含 2 個 InnerUserControl。 但我不知道如何在這種情況下進行綁定。 下面是我嘗試做的完整代碼。 請告知如何修復它以獲得與最后圖片相同的結果。 提前致謝!

主窗口.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>

主窗口.xaml

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

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

內部模型.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));
}

內部用戶控制.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>

內部用戶控制.xaml.cs

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

外部模型.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));
}

外部用戶控制.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>

歐爾UserControl.xaml.cs

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

工作綁定 MainWindow.xaml.cs

歡迎來到 SO!

這里有兩個問題,首先你的內部模型需要是屬性,所以把它們的聲明改成這樣:

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

第二個問題是你的綁定,你需要這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM