我有一个 wpf Window ( MyWindow),它DataContext设置为MyViewModel视图模型类:我的窗口.xaml<Window x:Class="MyProject.MyWindow" 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"> <MyUserControl Text="{Binding MyText, Mode=TwoWay}" /></Window>我的窗口.xaml.cspublic class MyWindow : Window{ MyWindow() { InitializeComponent(); DataContext = new MyViewModel(); }}视图模型.cs// MyViewModel implements IPropertyChangedpublic class MyViewModel : INotifyPropertyChanged{ public string MyText { get { ... } set { ... } } ...}我想将MyText属性的绑定传递MyWindow给以下 UserControl ( MyUserControl):我的用户控件.xaml<UserControl x:Class="MyProject.MyUserControl" 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"> <StackPanel> <TextBox Name="MainTextBox" Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" /> </StackPanel></UserControl>我的用户控件.xaml.cspublic partial class MyUserControl : UserControl { public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(MyUserControl), new UIPropertyMetadata(TextPropertyChangedHandler) );我想获得在键入的内容Textbox下MyUserControl进入MyViewModel.MyText。如何做到这一点?我试图绑定MyText到Text财产MyUserControl和随后的结合Text中TextBox,以Text从MyUserControl但这并不工作。
2 回答
- 2 回答
- 0 关注
- 363 浏览
添加回答
举报
0/150
提交
取消