Click here to Skip to main content
15,881,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, 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.
Posted
Updated 5-Sep-12 3:55am
v3
Comments
BillWoodruff 5-Sep-12 9:18am    
Did you mean to write: "if i add 2 user controls to my mainwindow i CANNOT access event from any one of the two controls." ? Please clarify exactly what you are experiencing here.

Suggest you post the code where you assign the EventHandlers to the RadioButtons.

best, Bill

User Control XAML:
XML
<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:
C#
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);
        }
    }

Main Window XAML:
XML
<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:
C#
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));
        }
    }
 
Share this answer
 
Comments
mryeti 5-Sep-12 14:28pm    
thank u all. one more question or suggestion how can i prevent overlapping of dynamically added user controls ?
Debashis Pal 6-Sep-12 0:32am    
If you place the dynamic controls inside a Panel like StackPanel, WrapPanel, etc. (As described in the example), overlapping should not occur. Also you must play with the Margin, HorizontalAlignment, VerticalAlignment of the control see the differences.
you can create custom event of your custom-control
and raise that event from radio-button's checked event
Happy coding!
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900