public UserControl1()
InitializeComponent();
DataContext = new UserControl1VM(); <-- HERE (Remove this)
所以WPF绑定引擎在UserControl1VM中寻找属性Text而不是MainWindowVM.删除设置DataContext并将UserControl1的XAML更新为:
<UserControl x:Class="DPandMVVM.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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="userControl1">
<TextBlock Text="{Binding TextInTextBlock, ElementName=userControl1}" />
</Grid>
</UserControl>
通过在UserControl上设置x:Name,使用ElementName绑定DP.
UPDATE
如果您希望UserControl的ViewModel完好无损,则必须更新MainWindow中的绑定.
显式告诉WPF绑定引擎在MainWindow的DataContext中使用ElementName查找属性,如下所示:
<local:UserControl1 TextInControl="{Binding DataContext.Text,
ElementName=mainWindow}" />
为此,您需要在窗口根级别设置x:Name =“mainWindow”.
出处:http://www.voidcn.com/article/p-udpyjbsd-bue.html
注意: 在使用MVVM框架的时候,如果在vm里获取UserControl的绑定数据,需要使用双向绑定"{Binding PageIndex,Mode=TwoWay}"
======================================================================
另外在提供一个在界面上使用ContentControl控件来绑定UserControl对象的方法。
界面元素:
<Window.DataContext>
<localVM:BarViewModel />
</Window.DataContext>
<StackPanel Height="30" Margin="0,300,2,105">
<ContentControl Content="{Binding StatusBar}"></ContentControl>
</StackPanel>
</Grid>
对应的BarViewModel类:
public class BarViewMod : INotifyPropertyChanged
public BarViewMod()
StatusBar = new View.UserCtrl.StatusBarView();
public object StatusBar { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
StatusBarView对于界面元素
<UserControl x:Class="WpfApp1.View.UserCtrl.StatusBarView"
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:WpfApp1.View.UserCtrl"
xmlns:uc="clr-namespace:WpfApp1.VM"
mc:Ignorable="d"
d:DesignHeight="25" d:DesignWidth="800">
<UserControl.DataContext>
<uc:StatusBarViewModel></uc:StatusBarViewModel>
</UserControl.DataContext>
<StatusBar x:Name="MyStatusBar">
<StatusBar.ItemsPanel>
<ItemsPanelTemplate>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</StatusBar.ItemsPanel>
<StatusBarItem>
<TextBlock Name="lbMsg" Text="{Binding TestMsg}" />
</StatusBarItem>
<Separator Grid.Column="1" />
<StatusBarItem Grid.Column="2">
<TextBlock Text="http://www.baidu.com/file.txt" />
</StatusBarItem>
<Separator Grid.Column="3" />
<StatusBarItem Grid.Column="4">
<ProgressBar Value="0" Width="90" Height="16" />
</StatusBarItem>
</StatusBar>
</Grid>
</UserControl>
StatusBarView对应的ViewModel类:
namespace WpfApp1.VM
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
public class StatusBarViewModel : INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
public string TestMsg { get; set; } = "test....";
如果是使用了其他框架的,可以依照框架修改,如下面是使用了IOC框架的,则在主界面的VM中构造函数中创建UserControl对象:
StatusBar = _serviceProvider.GetRequiredService<UserCtrl.StatusBarViewModel>();