如何将ListBox从UserControl传递到ManWindow [英] How Do I Pass ListBox from UserControl to ManWindow
本文介绍了如何将ListBox从UserControl传递到ManWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是WPF的新手,我需要一些关于将我的列表框从Usercontrol传递到MainWindow的帮助。
ListBox中的单词已经存储在数据库中,我只需要将ListBox传递给MainWindow。
这是我的UserControl.XAML。
Hi,
I am new to WPF and I need some help regarding passing my listbox from Usercontrol to MainWindow.
The words in the ListBox has been stored inside the database and I just need to pass the ListBox to the MainWindow.
This is my of UserControl.XAML.
<UserControl x:Name="Cloud1"
x:Class="WpfCloud.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="300" d:DesignWidth="300">
<Grid>
<ListBox Height="100" HorizontalAlignment="Left" Margin="73,80,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>
</UserControl>
这是我的UserControl.CS。
This is my UserControl.CS.
namespace WpfCloud
{
/// <summary>
/// Interaction logic for Cloudl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
ServiceSoapClient _service;
public UserControl1()
{
InitializeComponent();
_service = new iDigitalService.ServiceSoapClient();
}
//public string UserControlTextBlockTemplate
//{
// get { return TextBlockTemplate.Text; }
// set { TextBlockTemplate.Text = value; }
//}
private void UserControl1_Loaded(object sender, RoutedEventArgs e)
{
WordCloud cloud = _service.getLatestWordCloud();
PlayWord[] words = cloud.words;
foreach (PlayWord word in words)
{
string text = "(";
text += word.wordID.ToString();
text += ") ";
text += word.word;
text += " : ";
text += word.count.ToString();
listBox1.Items.Add(text);
}
}
}
}
这是我的MainWindow.XAML < br $>
This is my MainWindow.XAML
<Page x:Class="WpfCloud.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:WpfCloud;assembly=WpfCloud"
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"
WindowTitle="WordPlay"
Title="Page1"
Loaded="OnLoad"
d:DesignHeight="600" d:DesignWidth="800">
</Page>
是我的MainWindow.CS
This is my MainWindow.CS
namespace WpfCloud
{
/// <summary>
/// Interaction logic for Page1.xaml
/// </summary>
///
public partial class Page1 : Page
{
List<TextBlock> _list; //control for list it is a class command
public Page1()
{
InitializeComponent();
}
}
推荐答案
这是关于将一个Argument传递给一个类的全部内容。使用ListBox作为Argument为MainWindow类创建额外的contstructor。
UserControl
it's all about passing an Argument to a class. make use of creating additional contstructor for the MainWindow class with ListBox as Argument.
UserControl
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);
}
private void Window1_Loaded(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = new MainWindow(listBox1);
mainWindow.ShowDialog();
}
}
在MainWindow
In MainWindow
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ListBox _listBox = null;
public MainWindow()
{
InitializeComponent();
}
public MainWindow(ListBox listBox)
{
InitializeComponent();
_listBox = listBox;
}
}
这篇关于如何将ListBox从UserControl传递到ManWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文