我有一个名为UserControl
的{{1}},其中包含一个UserControl1
:
ListBox
<UserControl x:Class="myClass.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="400" d:DesignWidth="400">
<Grid>
<StackPanel Width="400">
<ListBox x:Name="lstBox_UserControl1"/>
</StackPanel>
</Grid>
</UserControl>
放在我的UserControl1
中,名为Window
:
MainWindow
我有一个简单的视图模型将<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" x:Class="myClass.MainWindow">
xmlns:uc="clr-namespace:myClass"
<Grid>
<StackPanel>
<UserControl x:Name="usrControl_TelegramQueue" Content="{Binding CurrentView}"/>
</StackPanel>
</Grid>
</Window>
绑定到UserControl1
中的UserControl
:
MainWindow
partial class UserControl1ViewModel : ViewModelBase {
private object _currentView;
UserControl1 UserControl1View= new UserControl1();
public TelegramQueueViewModel() {
CurrentView = UserControl1View;
}
public object CurrentView {
get { return _currentView; }
set {
_currentView = value;
OnPropertyChanged("CurrentView");
}
}
}
类仅处理属性更改事件:
ViewModelBase
现在,我还有一个名为 class ViewModelBase : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName) {
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
的{{1}},其中也包含一个Window
(称为SecondWindow
),类似于UserControl
的{{ 1}}。 UserControl2
打开MainWindow
,并且UserControl1
包含一个MainWindow
和一个SecondWindow
。选择UserControl2
的按钮后,我希望将其文本框的内容作为TextBox
添加到在主窗口中找到的Button
内。鉴于两个用户控件都包含在单独的窗口中,我该怎么做?