簡體   English   中英

捕捉按鈕單擊用戶控件

[英]Catch button click on user control

我有一個帶有按鈕和用戶控件的窗口。 現在,當我單擊按鈕(引發事件)時,我想抓住usercontrol。 我猜這個原理叫做事件隧道。 但是我也不知道,這種方法是否是捕獲按鈕單擊事件的正確方法。

<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>

現在,我想在從主窗口引發的usercontrol的部分類上捕獲事件。 我怎樣才能做到這一點?

namespace EventTunneling
{
    /// <summary>
    /// Interaction logic for Control.xaml
    /// </summary>
    public partial class Control : UserControl
    {
        public Control()
        {
            InitializeComponent();
        }
    }
}

不幸的是,您做錯了所有這些 首先,您需要將集合從后面的代碼數據綁定到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;
  1. 在窗口上進行公共活動。
  2. 單擊該按鈕時觸發事件。
  3. 通過控件訂閱事件

您只需在單擊按鈕時調用用戶Control中的公共方法即可:

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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM