【发布时间】:2014-06-24 09:40:49
【问题描述】:
我有一个带有按钮和用户控件的窗口。现在,当我单击按钮时,引发的事件,我想抓住用户控件。我猜这个原理叫做事件隧道。但我也不知道,这种方式是否是捕捉按钮点击事件的正确方式。
例子
<Window x:Class="EventTunneling.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:EventTunneling"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Click me"></Button>
<uc:Control Grid.Row="1"></uc:Control>
</Grid>
</Window>
用户控制
<UserControl x:Class="EventTunneling.Control"
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>
</Grid>
</UserControl>
现在我想捕捉从主窗口引发的部分用户控件类上的事件。我该怎么做?
namespace EventTunneling
{
/// <summary>
/// Interaction logic for Control.xaml
/// </summary>
public partial class Control : UserControl
{
public Control()
{
InitializeComponent();
}
}
}
【问题讨论】:
-
你为什么要这样做?您实际上想要达到什么目的?
-
我在用户控件上有一个数据网格和一个对象集合作为这个数据网格的数据上下文。在主窗口上,我有一个保存按钮。当我点击保存按钮时,我想将对象集合保存为 xml。