Click here to Skip to main content
11,638,758 members (72,642 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF User-controls
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 1:13am
Edited 5-Sep-12 3:55am
v3
Comments
BillWoodruff at 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!
Smile | :)
  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">
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">
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 at 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 at 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
0 Sergey Alexandrovich Kryukov 314
1 stibee 233
2 Suvendu Shekhar Giri 230
3 Richard Deeming 190
4 Mika Wendelius 178
0 OriginalGriff 9,061
1 Sergey Alexandrovich Kryukov 8,773
2 Mika Wendelius 7,027
3 Suvendu Shekhar Giri 2,494
4 F-ES Sitecore 2,438


Advertise | Privacy | Mobile
Web03 | 2.8.150728.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2015
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