2017-08-10 89 views
-1

我想初始化usercontrol属性usercontrol属性..下面是快照...我想通过usercontrol初始化父“一”和“两个”属性如果属性是nullvice-versa从usercontrol初始化父ViewModel属性datacontext属性

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; } 
    } 
} 
+0

你为什么要设置的UserControl1 DataContext属性在构造函数中:

然后,您可以使用Window.GetWindow方法来访问父窗口的DataContext? – mm8

+0

@ mm8因为我为datacontext属性发送空值 – Moumit

+0

因此,您想在UserControl1的构造函数中设置ParentViewModel的One和Two属性,或者您的问题是什么? – mm8

回答

0

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

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... 
       } 
      } 
     } 
    }; 
}