【问题标题】:Initialize parent ViewModel property from usercontrol datacontext property从 usercontrol datacontext 属性初始化父 ViewModel 属性
【发布时间】:2017-08-10 20:26:39
【问题描述】:

我正在尝试从usercontrol 属性初始化父ViewModel 属性。下面是快照...如果属性是null,我想通过用户控件初始化父“一”和“二”属性或vice-versa

window.xaml

<Window x:Class="wpfParentUserControlDemo.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:wpfParentUserControlDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="369.737" Width="525">
    <Window.DataContext>
        <local:ParentViewModel />
    </Window.DataContext>
    <Grid Margin="0,0,0,-4">        
        <local:UserControl1 DataContext="{Binding One}" Margin="288,49,22,240"></local:UserControl1>
        <local:UserControl1 DataContext="{Binding Two}" Margin="10,49,283,240"></local:UserControl1>
    </Grid>
</Window>

MainWindow.cs

using System.Windows;

namespace wpfParentUserControlDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class ParentViewModel : BaseViewModel
    {
        private DemoConnectionViewModel _One;

        public DemoConnectionViewModel One
        {
            get { return _One; }
            set { _One = value; }
        }

        private DemoConnectionViewModel _Two;

        public DemoConnectionViewModel Two
        {
            get { return _Two; }
            set { _Two = value; }
        }
    }
}

UserControl1.Xaml

<UserControl x:Class="wpfParentUserControlDemo.UserControl1"
             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:wpfParentUserControlDemo"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <ComboBox ItemsSource="{Binding Connections}" DisplayMemberPath="Name" ></ComboBox>
    </Grid>
</UserControl>

UserControl1.cs

using System.Collections.ObjectModel;
using System.Data;
using System.Windows.Controls;

namespace wpfParentUserControlDemo
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();

            if (this.DataContext == null)
            {
                var ViewModel = new DemoUserMainViewModel();
                ViewModel.Connections = new ObservableCollection<DemoConnectionViewModel>();
                ViewModel.Connections.Add(new DemoConnectionViewModel { Name = "A", Type = SqlDbType.BigInt });
                ViewModel.Connections.Add(new DemoConnectionViewModel { Name = "B", Type = SqlDbType.Bit });
                ViewModel.Connections.Add(new DemoConnectionViewModel { Name = "C", Type = SqlDbType.NVarChar });

                this.DataContext = ViewModel;
            }
        }
    }

    public class DemoUserMainViewModel : BaseViewModel
    {
        private ObservableCollection<DemoConnectionViewModel> _Connections;

        public ObservableCollection<DemoConnectionViewModel> Connections
        {
            get { return _Connections; }
            set { _Connections = value; }
        }
    }

    public class DemoConnectionViewModel : BaseViewModel, IDataConnection
    {
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private SqlDbType _Type;

        public SqlDbType Type
        {
            get { return _Type; }
            set { _Type = value; }
        }
    }
}

IDataConnection.cs

using System.Data;

namespace wpfParentUserControlDemo
{
    public interface IDataConnection
    {  
        string Name { get; set; }
        SqlDbType Type { get; set; }
    }
}

【问题讨论】:

  • 那你为什么要在构造函数中设置 UserControl1 的 DataContext 属性呢?
  • @mm8 因为我正在为 datacontext 属性发送 null
  • 那么你想在 UserControl1 的构造函数中设置 ParentViewModel 的 One 和 Two 属性,或者你的问题是什么?
  • @mm8 是的.. 更新问题请检查

标签: c# wpf xaml mvvm user-controls


【解决方案1】:

我不确定你要做什么,但如果你想检查UserControlDataContext 是否已设置,你应该等到它被加载。

然后您可以使用Window.GetWindow 方法访问父窗口的DataContext

public UserControl1()
{
    InitializeComponent();
    this.Loaded += (s, e) =>
    {
        if (this.DataContext == null)
        {
            MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
            if (parentWindow != null)
            {
                ParentViewModel parentViewModel = parentWindow.DataContext as ParentViewModel;
                if (parentViewModel != null)
                {
                    //set One or Two or do whatever you want to do here...
                }
            }
        }
    };
}

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2021-04-16
    相关资源
    最近更新 更多