c# - 从另一个窗口的 UserControl WPF 将 ListBoxItem 添加到窗口的 UserControl 中的 ListBox
问题描述
我有UserControl
一个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
in 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));
}
}
}
现在,我有另一个Window
叫它SecondWindow
,其中也有一个UserControl
(称为UserControl2
),类似于 how MainWindow
has UserControl1
。MainWindow
打开SecondWindow
并UserControl2
包含 aTextBox
和 a Button
。选择的按钮UserControl2
被选择时,我希望在主窗口中找到ListBoxItem
其文本框的内容。ListBox
鉴于两个用户控件都包含在单独的窗口中,我该如何做呢?
解决方案
推荐阅读
- java - 在 Spring Boot 中接受来自特定 ip 的请求
- r - 从文件路径中获取第一个元素
- android - Jacoco 的覆盖率为 0%,而 Intellij Idea 的覆盖率非常低
- r - UseMethod(“cld”)中的错误:没有适用于“cld”的方法应用于“data.frame”类的对象
- c - RGB 到 HIS 为某些 RGB 值提供 NaN
- c# - 有什么办法可以解决两个同时调用两个不同的类互相等待的问题
- php - 用户 ID 和用户名未插入关系
- extjs - Ext.container.Container / ExtJS:如何通过id跳转到元素?
- python - ImportError:没有名为“_bz2”的模块
- python - 使用 Inspect 提取函数的参数