본문 바로가기
C#(CSharp)/WPF

사용자 정의 이벤트 만들기

by swconsulting 2015. 4. 8.

[XAML]

<UserControl x:Class="CommonUserControl.CommonCtrl.CustomEventUserControl"

             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"

             Width="100" Height="100">

    <Grid >

        <Button Background="Blue" Click="Button_Click"></Button>

    </Grid>

</UserControl>

 


[CS]

namespace CommonUserControl.CommonCtrl

{

    public class CustomEventCtrlArgs : RoutedEventArgs

    {

        public CustomEventCtrlArgs(RoutedEvent routedEvent, object source)

            : base(routedEvent, source)

        { }

    }

 

    /// <summary>

    /// Interaction logic for CustomEventUserControl.xaml

    /// </summary>

    public partial class CustomEventUserControl : UserControl

    {

        public static readonly RoutedEvent CustomCtrlEvent =

            EventManager.RegisterRoutedEvent("CustomCtrl"RoutingStrategy.Bubble,

            typeof(RoutedEventHandler), typeof(CustomEventUserControl));

 

        public event RoutedEventHandler CustomCtrl

        {

            add { AddHandler(CustomCtrlEvent, value); }

            remove { RemoveHandler(CustomCtrlEvent, value); }

        }

       

        public CustomEventUserControl()

        {

            InitializeComponent();

        }

 

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            RaiseEvent(new CustomEventCtrlArgs(CustomEventUserControl.CustomCtrlEvent, sender));

        }

 

       

    }

}

 

 

<Window x:Class="SWConsultingStickyNote.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:noteItems="clr-namespace:SWConsultingStickyNote.UICommon"       

        xmlns:locText="clr-namespace:Localization.Resx;assembly=Localization"       

        xmlns:custom="clr-namespace:CommonUserControl.CommonCtrl;assembly=CommonUserControl"

        WindowStartupLocation="CenterScreen"

        Icon="Images/MainAppIcon.ico"               

        Title="{x:Static locText:Resources.Main_Title}"

        Height="400" Width="600">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="30" />           

            <RowDefinition Height="*" />

        </Grid.RowDefinitions>

        <Border Grid.Row="0" BorderThickness="1">

            <Grid>

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="auto" />

                    <ColumnDefinition Width="auto" />

                </Grid.ColumnDefinitions>

                <CheckBox Grid.Column="0"

                          x:Name="xWindowsStartUp"

                          IsChecked="{Binding RunAtWindowsStartup, Mode=TwoWay}"

                          Content="Run at windows startup"                 

                          />

                <WrapPanel Grid.Column="1">

                    <TextBlock Text=", Note Count : "></TextBlock>

                    <TextBlock Text="{Binding NoteCount, Mode=TwoWay}"></TextBlock>

                    <custom:CustomEventUserControl />

                </WrapPanel>

            </Grid>

        </Border>

 

 


참고 URL 

https://msdn.microsoft.com/ko-kr/library/ms752288(v=vs.110).aspx

http://stackoverflow.com/questions/4698047/event-bubbling-for-custom-event-in-wpf