如何在wpf中的用户控件中订阅事件 [英] how To subsribe to event in user control in wpf

查看:146
本文介绍了如何在wpf中的用户控件中订阅事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个由3个单选按钮组成的用户控件.我为用户控件中的每个单选按钮创建了一个已检查事件.如果我在主窗口中添加2个或更多用户控件,则可以从任何一个控件访问事件.我怎么解决这个问题.
我要在按钮单击时动态添加用户控件.

i am creating a user control which consist of 3 radio button. i created a checked event for each radio button in user control.if i am adding 2 or more user control in my mainwindow i can access event from any one control. how can i solve this problem.
i am adding the user control dynamically on button click.

推荐答案

您可以创建自定义控件的自定义事件
并从单选按钮的选中事件引发该事件
祝您编程愉快!
:)
you can create custom event of your custom-control
and raise that event from radio-button''s checked event
Happy coding!
:)


用户控件XAML:
User Control XAML:
<usercontrol x:class="WpfApplication1.UserControl1" xmlns:x="#unknown">
             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="103" d:DesignWidth="134"
             Background="AliceBlue"
             Margin="10">
    <grid height="81" width="111" background="AntiqueWhite">
        <radiobutton content="Option A" height="16" horizontalalignment="Left" margin="12,9,0,0" name="radioButtonA" verticalalignment="Top" checked="radioButton_CheckedChanged" />
        <radiobutton content="Option B" height="16" horizontalalignment="Left" margin="12,31,0,0" name="radioButtonB" verticalalignment="Top" checked="radioButton_CheckedChanged" />
        <radiobutton content="Option C" height="16" horizontalalignment="Left" margin="12,53,0,0" name="radioButtonC" verticalalignment="Top" checked="radioButton_CheckedChanged" />
    </grid>
</usercontrol>


用户控制代码:


User Control Code:

public partial class UserControl1 : UserControl
    {
        public delegate void OptionChangedEventHandler(UserControl1 sender, RadioButton radioButton, bool? checkedState);
        public event OptionChangedEventHandler OptionChanged;

        public UserControl1()
        {
            InitializeComponent();
        }

        public string DisplayName { get; set; }

        private void radioButton_CheckedChanged(object sender, RoutedEventArgs e)
        {
            if (this.OptionChanged != null)
                this.OptionChanged(this, sender as RadioButton, (sender as RadioButton).IsChecked);
        }
    }


主窗口XAML:


Main Window XAML:

<window x:class="WpfApplication1.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <grid>
        <stackpanel>
            <button content="Add User Control" click="Button_Click" height="30" />
            <wrappanel name="wrapPanel" orientation="Vertical" />
        </stackpanel>
    </grid>
</window>


主窗口代码:


Main Window Code:

public partial class MainWindow : Window
    {
        private static int i = 0;
        
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            UserControl1 uc = new UserControl1() { DisplayName = "User Control " + (i++).ToString() };
            uc.OptionChanged+=new UserControl1.OptionChangedEventHandler(uc_OptionChanged);
            wrapPanel.Children.Add(uc);
        }

        void uc_OptionChanged(UserControl1 sender, RadioButton radioButton, bool? checkedState)
        {
            MessageBox.Show(String.Format("'{0}' = '{1}' for User Control '{2}'", radioButton.Content, checkedState, sender.DisplayName));
        }
    }


这篇关于如何在wpf中的用户控件中订阅事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆