none
Custom event RRS feed

Answers

  • Yes, it is possible to load it dynamically, but you need to host it somewhere. Let's assume you have a panel (e.g. Grid) in your XAML:

    <Grid Name="dynamicHost" />
    And now you may add some dymamic control to this panel:

    public Window1()
    {
       InitializeComponent();
       // ...
       UC_AllClients uclb = new UC_AllClients();
       uclb.IDTextChanged += UCIDTextChanged;
       uclb.Height = double.NaN; // this is for "Auto"
       dynamicHost.Children.Add(uclb); // populate to UI
    }

    • Marked as answer by nitinca Monday, August 24, 2009 9:46 PM
    Monday, August 24, 2009 9:06 PM
  • UserControl uclb = Application.LoadComponent(new Uri("UserControls/UC_AllClients.xaml", System.UriKind.RelativeOrAbsolute)) as UserControl;
    EventInfo evt = uclb.GetType().GetEvent("IDTextChanged");
    if (evt != null)
      evt.AddEventHandler(uclb, new TextChangedEventHandler(UCIDTextChanged));

    • Marked as answer by nitinca Sunday, September 13, 2009 7:49 PM
    Sunday, September 13, 2009 7:20 PM

All replies

  • Not sure... but maybe you need a public event?....
    Kenneth
    Monday, August 24, 2009 6:23 PM
  • I can try making it public. I am wondering if anyone else have seen simillar behavior.
    Monday, August 24, 2009 6:26 PM
  • What do you mean "private instance"? 
    If I got your point from the other thread, it is a private variable defined inside a Window class without definition in markup? 
    And why should it be fired, if it is not even visible in the interface?
    Monday, August 24, 2009 7:36 PM
  • Yes it was..so what is the solution to get it fired?
    Monday, August 24, 2009 8:11 PM
  • First of all, it should be displayed somewhere in the Window the user to be able to change its contents. If it is not displayed and text is not explicitly changed from code, there's no any reason the event to be fired.
    Monday, August 24, 2009 8:16 PM
  • Can you please show me how..Here is the code..

    The reason I am asking this is code..if I take out code in bold, it wont work..can you please explain?

    Window1.xaml

    <Grid>
            ....
            ....
            <local:UC_AllClients x:Name="UC_ListBox" Height="Auto" IDTextChanged="UCIDTextChanged"/>
            ....
            ....
             <TextBox x:Name="txtID" TextChanged="txtID_TextChanged" />
            ....
            ....
    </Grid>

    Window1.xaml.cs

    public

    partial class Window1: Window
       {
        ...
        private
    UC_AllClients uclb = new UC_AllClients();
        ...
          public Window1()
             {
                InitializeComponent();
                uclb.IDTextChanged += new TextChangedEventHandler(UCIDTextChanged);
              ...}

            private void UCIDTextChanged(object sender, TextChangedEventArgs e)
                  {
                     MessageBox.Show("test");

                   }

     

    Monday, August 24, 2009 8:31 PM
  • Instance of UC_AllClients named "UC_ListBox" is defined in XAML, and code in bold adds an event handler to this instance's IDTextChanged event.

    Variable uclb in code is initialized with a new instance of UC_AllClients, not those one was declared in XAML. And event handler is now attached to that new instance, which is not displayed in UI and never getting changed.

    If you want to set event handler in code, you should attach it to the instance declared in XAML:

    public Window1()
    {
       InitializeComponent();
       UC_ListBox.IDTextChanged += new TextChangedEventHandler(UCIDTextChanged);
    }

    Monday, August 24, 2009 8:43 PM
  • Thats how it is now. Acutally I am trying to design "Shell form" simillar to as what is described in Sams teach yourself WPF in 24 hours by Robert Eisenberg. 

    The left side of the form have listbox(usercontrol) and there is one more usecontrol for main content of the form. Right now this form works fine as you mentioned or as per my other thread. Now I want to create simillar usercontrols for listbox and main content for other objects and use the same shell form to load it.

    Is it possible to load usercontrol dynamic rather than hardcoding like this?

         <local:UC_AllClients x:Name="UC_ListBox" Height="Auto" IDTextChanged="UCIDTextChanged"/>


    Monday, August 24, 2009 8:55 PM
  • Yes, it is possible to load it dynamically, but you need to host it somewhere. Let's assume you have a panel (e.g. Grid) in your XAML:

    <Grid Name="dynamicHost" />
    And now you may add some dymamic control to this panel:

    public Window1()
    {
       InitializeComponent();
       // ...
       UC_AllClients uclb = new UC_AllClients();
       uclb.IDTextChanged += UCIDTextChanged;
       uclb.Height = double.NaN; // this is for "Auto"
       dynamicHost.Children.Add(uclb); // populate to UI
    }

    • Marked as answer by nitinca Monday, August 24, 2009 9:46 PM
    Monday, August 24, 2009 9:06 PM
  • Wow. Great. Thank you so much for your help. I really appriciate it.

    Monday, August 24, 2009 9:17 PM
  • Hi Experts,
                   Need little more help. Above thing works fine but I am trying to initialize usercontrol it differently. how can I implement custom event? 
    instead of
                 UC_AllClients uclb = new UC_AllClients();

    I am initializing like this..
                UserControl uclb = Application.LoadComponent(new Uri("UserControls/UC_AllClients.xaml", System.UriKind.RelativeOrAbsolute)) as UserControl;
    
    in this case How can use custom event? (uclb.IDTextChanged += UCIDTextChanged;)

    Please help.

    Thank you

    Nitin
    Sunday, September 13, 2009 6:03 PM
  • You mean you don't know the exact type of control loaded and want to subscribe to its event? 

    Then you have two ways: 
    1. Get event by name using reflection and then subscribe your handler to it.
    2. Declare your dynamically loaded controls implementning a specific interface with event you need and then just cast to this interface type.

    Loading absolutely random control is a rare case, so the second way is preferred.
    Sunday, September 13, 2009 6:32 PM
  • No..I guess how to attach event to the instance uclb.  
    If I instantiate like usercontrol like this UC_AllClients uclb = new UC_AllClients(); I can attach like this uclb.IDTextChanged += UCIDTextChanged;

    But since I am instantiating as a user control  
    UserControl uclb = Application.LoadComponent(new Uri("UserControls/UC_AllClients.xaml", System.UriKind.RelativeOrAbsolute)) as UserControl;
    , it doesnt recognise event IDTextChanged from the user control

    I tried 
                TextChangedEventHandler IDTextChanged = (TextChangedEventHandler)uc.FindName("IDTextChanged");
                uclb.IDTextChanged += UCIDTextChanged;
    it wont compile saying undeclared component

    Not sure how to do the way you suggested.
    Sunday, September 13, 2009 6:56 PM
  • Do you know the type you're going to instantiate at compile time? If yes, just make variable of that type (instead of UserControl) and cast LoadComponent() to it.
    Sunday, September 13, 2009 7:07 PM
  • no it doesnt know compile time..All I am trying to do is get notification when text box content in the user control is changed , and get the changed value. Do you think using delegates might solve the problem? I have never used delegates before.
    Sunday, September 13, 2009 7:13 PM
  • UserControl uclb = Application.LoadComponent(new Uri("UserControls/UC_AllClients.xaml", System.UriKind.RelativeOrAbsolute)) as UserControl;
    EventInfo evt = uclb.GetType().GetEvent("IDTextChanged");
    if (evt != null)
      evt.AddEventHandler(uclb, new TextChangedEventHandler(UCIDTextChanged));

    • Marked as answer by nitinca Sunday, September 13, 2009 7:49 PM
    Sunday, September 13, 2009 7:20 PM
  • Yup..Works fine. Thank you so much. This will help me writing dynamic forms.
    Sunday, September 13, 2009 7:48 PM