我有两个名为
的用户控件在一个主窗口中
在第二个用户控件中,我正在使用数据网格。在更改数据网格中的元素时,我必须能够在Welcome
用户控件中设置值
在Welcome
用户控件
<StackPanel Orientation="Horizontal">
<Label Content="Name:" FontWeight="Bold" Name="lblClientName" />
<TextBox Name="txtClientName" Width="85"
Background="Transparent" IsReadOnly="True"/>
</StackPanel>
在Data
用户控件
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Here on change of this event i must be able to display
// data in textbox i.e.txtClientName
}
答案 0 :(得分:0)
这是非常简单的事情只需要一些.net框架的基本知识..我希望这可以帮到你。我刚刚在Data usercontrol中创建了一个自定义事件,它会冒泡行事件。
数据用户控制代码:
public partial class Data : UserControl
{
private event EventHandler _RowSelectionChanged;
public event EventHandler RowSelectionChanged
{
add { _RowSelectionChanged += value; }
remove { _RowSelectionChanged -= value; }
}
private void RaiseSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_RowSelectionChanged != null)
_RowSelectionChanged(sender, e);
}
public Data()
{
InitializeComponent();
}
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
RaiseSelectionChanged(sender, e);
}
}
欢迎使用UserControl代码:
public partial class Welcome : UserControl
{
public Welcome()
{
InitializeComponent();
}
public string ClientName
{
get
{
return txtClientName.Text;
}
set
{
txtClientName.Text = value;
}
}
}
主窗口类:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ucData.RowSelectionChanged += new EventHandler(ucData_RowSelectionChanged);
}
void ucData_RowSelectionChanged(object sender, EventArgs e)
{
var ev = e as SelectionChangedEventArgs;
var grid = sender as DataGrid;
ucWelcome.ClientName = "any thing";
//this is how you can change Welcome UserControl
}
}
问候。