Click here to Skip to main content
13,248,766 members (64,709 online)
Rate this:
 
Please Sign up or sign in to vote.
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 5-Sep-12 2:13am
Updated 5-Sep-12 4: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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

you can create custom event of your custom-control
and raise that event from radio-button's checked event
Happy coding!
:)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

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));
        }
    }
  Permalink  
Comments
aditya_ingle 5-Sep-12 14:28pm
   
thank u all. one more question or suggestion how can i prevent overlapping of dynamically added user controls ?
Member 3643694 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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171114.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100