目标:在UserContrl空间中有一个Button,点击该Button,忘Window1中的listbox中添加数据
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication2
{
/// <summary>
/// Monitor_Config.xaml 的交互逻辑
/// </summary>
public partial class Monitor_Config : UserControl
{
public delegate void ExecuteButtonHandler(Object Sender, ExecuteButtonEventArgs e);
public event ExecuteButtonHandler ExecuteButton;
private Monitor _monitor = new Monitor(100,"100号话务员");
public Monitor_Config()
{
InitializeComponent();
}
private void btn_exc_Click(object sender, RoutedEventArgs e)
{
ExecuteButtonEventArgs ve = new ExecuteButtonEventArgs(_monitor);
onExebtn(this, ve);
}
protected void onExebtn(Object sender, ExecuteButtonEventArgs e)
{
if (ExecuteButton != null)
{
ExecuteButton(sender, e);
}
}
}
public class ExecuteButtonEventArgs : EventArgs {
public ExecuteButtonEventArgs(Monitor _monitor)
{
this.Monitor = _monitor;
}
public Monitor Monitor { get; set; }
}
}
说明:定义委托、事件、及ExecuteButtonEventArgs,在btn_exc_Click中触发;
Window1.xaml:
代码
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:conig="clr-namespace:WpfApplication2"
Title="Window1" Height="500" Width="600">
<Grid>
<ListBox Margin="13,9,0,0" Name="listBox1" ItemsSource="{Binding}" Height="100" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<conig:Monitor_Config HorizontalAlignment="Right" Width="230" Height="250" VerticalAlignment="Top" x:Name="config_control"></conig:Monitor_Config>
</Grid>
</Window>
Window.xaml.cs
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace WpfApplication2
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
private ObservableCollection<Monitor> _list = new ObservableCollection<Monitor>();
private List<string> list_string = new List<string>();
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);
_list.Add(new Monitor(1, "1号"));
_list.Add(new Monitor(2, "2号"));
_list.Add(new Monitor(3, "3号"));
_list.Add(new Monitor(4, "4号"));
_list.Add(new Monitor(5, "5号"));
listBox1.DataContext = _list;
this.config_control.ExecuteButton += new Monitor_Config.ExecuteButtonHandler(config_control_ExecuteButton);
}
void config_control_ExecuteButton(object Sender, ExecuteButtonEventArgs e)
{
Monitor monitor = e.Monitor;
_list.Add(monitor);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
}
public ObservableCollection<Monitor> List
{
get { return _list; }
set { _list = value; }
}
}
public class Monitor {
public Monitor(int id ,string name) {
this._id = id;
this._name = name;
}
private int _id;
public int ID {
get { return _id; }
set { _id = value; }
}
private string _name;
public string Name {
get { return _name; }
set { _name = value; }
}
}
}
完成了

浙公网安备 33010602011771号