从另一个窗口的UserControl WPF将ListBoxItem添加到窗口的UserControl中的ListBox中

时间:2019-07-03 12:11:23

标签: c# wpf xaml

我有一个名为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内。鉴于两个用户控件都包含在单独的窗口中,我该怎么做?

0 个答案:

没有答案