我有一個 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) );我想獲得在鍵入的內(nèi)容Textbox下MyUserControl進入MyViewModel.MyText。如何做到這一點?我試圖綁定MyText到Text財產(chǎn)MyUserControl和隨后的結合Text中TextBox,以Text從MyUserControl但這并不工作。
- 2 回答
- 0 關注
- 381 瀏覽
添加回答
舉報
0/150
提交
取消