[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
'C#(CSharp) > WPF' 카테고리의 다른 글
결합 속성(?) Attached Property (0) | 2015.04.08 |
---|---|
의존 속성(Dependency Object) (0) | 2015.04.08 |
XAML Custom Namespace (0) | 2015.04.08 |
다국어 작업(Globalization, Localization) (0) | 2015.04.02 |
Mutex를 이용해서 1개의 프로세스만 동작하도록 하기 (0) | 2015.03.22 |