C# 从usercontrol datacontext属性初始化父ViewModel属性
我正在尝试从C# 从usercontrol datacontext属性初始化父ViewModel属性,c#,wpf,xaml,mvvm,user-controls,C#,Wpf,Xaml,Mvvm,User Controls,我正在尝试从usercontrol属性初始化父ViewModel属性。。下面是快照。。。如果属性为null或反之亦然,我想通过usercontrol初始化父属性“one”和“Two” window.xaml <Window x:Class="wpfParentUserControlDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x
usercontrol
属性初始化父ViewModel
属性。。下面是快照。。。如果属性为null
或反之亦然,我想通过usercontrol初始化父属性“one”和“Two”
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>
<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>
我不确定您想做什么,但如果您想检查
UserControl
的DataContext
是否已设置,您应该等到它已加载
然后,您可以使用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...
}
}
}
};
}
那么,为什么要在构造函数中设置UserControl1的DataContext属性?@mm8,因为我为DataContext属性发送null,所以您想在UserControl1的构造函数中设置ParentViewModel的一个和两个属性,或者您的问题是什么?@mm8是。。更新了问题,请检查
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; }
}
}
}
using System.Data;
namespace wpfParentUserControlDemo
{
public interface IDataConnection
{
string Name { get; set; }
SqlDbType Type { get; set; }
}
}
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...
}
}
}
};
}