从usercontrol datacontext属性初始化父ViewModel属性

时间:2017-08-10 20:26:40

标签: c# wpf xaml mvvm user-controls

我正在尝试从ViewModel属性初始化父usercontrol属性..以下是快照...我想初始化父"一个"和"两个" usercontrol的属性,如果属性为nullvice-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; }
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定您要做什么,但如果您想检查DataContext的{​​{1}}是否已设置,则应等到它已加载。

然后,您可以使用UserControl方法访问父窗口的DataContext

Window.GetWindow