【问题标题】:Catch button click on user control捕获按钮单击用户控件
【发布时间】: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。

标签: c# wpf


【解决方案1】:

不幸的是,您这样做所有都是错误的。首先,您需要将您的集合从后面的代码数据绑定到DataGrid.ItemsSource 属性。然后,当点击Button 时,只需在后面的代码中处理它,您可以在其中访问该集合并简单地保存它:

UserControl:

<DataGrid ItemsSource="{Binding Items}" ... />

MainWindow:

<Button Grid.Row="0" Content="Click me" Click="Button_Click" />

在后面的代码中(你应该在这个属性上实现INotifyPropertyChanged 接口):

public ObservableCollection<YourDataType> Items { get; set; }

...

private void Button_Click(object sender, RoutedEventArgs e)
{
    SaveAsXml(Items);
}

在构造函数后面的代码中(或以您喜欢的任何有效方式设置它):

DataContext = this;

【讨论】:

    【解决方案2】:
    1. 在窗口上创建一个公共事件。
    2. 点击按钮时触发事件。
    3. 从您的控件订阅事件

    【讨论】:

      【解决方案3】:

      您可以在单击按钮时简单地执行此操作,从而调用用户控件中的公共方法:

      namespace EventTunneling
      {
          public partial class Control : UserControl
          {
            public Control()
            {
                InitializeComponent();
            }
      
            public void CatchEventOnUserControl()
            {
                //do your job here
                //....
            }
          }
      }
      

      然后当点击按钮时调用 CatchEventOnUserControl 方法

      <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" Click="Button_Click"></Button>
          <uc:Control x:Name="mycontrol" Grid.Row="1"></uc:Control>
      </Grid>
      

      而后面的代码是

      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
      
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              mycontrol.CatchEventOnUserControl();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-05-21
        • 1970-01-01
        • 2011-02-28
        • 1970-01-01
        • 1970-01-01
        • 2011-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多