C# 将模型从窗口绑定到用户控件
我需要以双向模式将代码隐藏中的C# 将模型从窗口绑定到用户控件,c#,wpf,C#,Wpf,我需要以双向模式将代码隐藏中的“CommonViewModel”对象与“MainWindow”及其子对象“UserControl1”和“UserControl2”绑定 MainWindow.xaml <Window x:Class="Testing.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.co
“CommonViewModel”
对象与“MainWindow”
及其子对象“UserControl1”
和“UserControl2”
绑定
MainWindow.xaml
<Window x:Class="Testing.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:Testing"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<local:UserControl1/>
<local:UserControl2/>
</StackPanel>
</Window>
UserControl1.xaml
<UserControl x:Class="Testing.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">
<StackPanel >
<TextBox Text="{Binding CommonProperity1,Mode=TwoWay}"/>
</StackPanel>
</UserControl>
很抱歉,是的,你是对的,它工作正常,错误在框架内,代码没有问题
无论如何,感谢您的帮助。您的代码应该可以工作。不是吗?如果在子控件上未设置其他DataContext,则父控件的DataContext将由子控件继承。调试输出中是否存在绑定错误?您的代码工作正常。你面临什么样的问题?
<UserControl x:Class="Testing.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">
<StackPanel >
<TextBox Text="{Binding CommonProperity1,Mode=TwoWay}"/>
</StackPanel>
</UserControl>
<UserControl x:Class="Testing.UserControl2"
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">
<StackPanel>
<TextBox Text="{Binding CommonProperity2,Mode=TwoWay}"/>
</StackPanel>
</UserControl>
public class CommonViewModel : NotifyChangeClass
{
private string _Localtextdata1;
private string _Localtextdata2;
public string CommonProperity1
{
get { return _Localtextdata1; }
set
{
_Localtextdata1 = value;
NotifyPropertyChange("CommonProperity1");
}
}
public string CommonProperity2
{
get { return _Localtextdata2; }
set
{
_Localtextdata2 = value;
NotifyPropertyChange("CommonProperity2");
}
}
}
public class NotifyChangeClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChange(string ProperityName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(ProperityName));
}
}
}