views:

40

answers:

2

How to raise a event in usercontrol from the another usercontrol. i tried to do with delegates, but it doesnt work. How can i do this. am using C#(WPF)

usercontrol1.cs

 public partial class UserControl1 : UserControl
{
    delegate void myDelegate();



    public UserControl1()
    {
        InitializeComponent();


    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        UserControl2 obj = new UserControl2();

        myDelegate d = new myDelegate(obj.CallDelegate);
        obj.CallDelegate();

    }
}

Usercontrol2.cs

 public partial class UserControl2 : UserControl
{

    public UserControl2()
    {
        InitializeComponent();
    }

    public void CallDelegate()
    {
        this.Width = 50;
        this.Height = 50;
        MessageBox.Show("Method called ");
    }

}

when i use delegate i can go get the messagebox from the method, but the control doesnt resize. do i want to render it again ?? i have tried to assign explicitly, but doesnt work

+1  A: 

In general, only the event owner can raise an event. There are exceptions to this (such as with Button.PerformClick in Windows Forms, but they have to be specifically provided by the class in question.

It's possible that WPF routed events may give an alternative here, but you haven't been clear about what kind of events you're talking about. An example of what you're trying to do would be helpful.

Jon Skeet
Hai Jon, I cant understand fully from your saying, mean time am also dosent mention the event, i have attached a sample here. http://cid-08ec3041618e8ee4.office.live.com/self.aspx/blog/delegates.rar
deep
there are two user controls in the form, when i click the resize in one user control i want to rise the event to resize the another usercontrol
deep
@deep: It would be better to edit the code - just a small sample - into your question, rather than just post a rar file. I suspect you'll find it hard to make one control *just* raise the resize event of a different control... but is the second control *actually* being resized anyway? Can you just set the size explicitly?
Jon Skeet
@Jon: I have edited my question with the sample code i used. kindly take a lool
deep
+1  A: 

that's because in your code you raise an event on a new UserControl2. for your specific example the code of the UserControl1.button1_Click event should be like this:

private void button1_Click(object sender, RoutedEventArgs e)
{
    if (this.Parent != null && this.Parent is StackPanel)
    {
        StackPanel parentControl = this.Parent as StackPanel;
        foreach (UIElement child in parentControl.Children)
        {
            if (child is UserControl2)
                ((UserControl2)child).CallDelegate();
        }
    }
}

EDIT: kay so it seems you want to get all the usercontrol2 within the window1 to be resized. then what you need is to make a recursive function to get the topmost parent, e.g (modded from hardcodet.net/2008/02/find-wpf-parent)

DependencyObject GetHighestParent(DependencyObject child)
{
    ContentElement contentElement = child as ContentElement;
    if (contentElement != null)
    {
        DependencyObject parent = ContentOperations.GetParent(contentElement);
        if (parent != null) return parent;

        FrameworkContentElement fce = contentElement as FrameworkContentElement;
        return fce != null ? fce.Parent : null;
    }

    FrameworkElement frameworkElement = child as FrameworkElement;
    if (frameworkElement != null)
    {
        DependencyObject parent = frameworkElement.Parent;
        if (parent != null)
        {
            return GetHighestParent(parent);
        }
        else
        {
            return child;
        }
    }

    DependencyObject visualParent = VisualTreeHelper.GetParent(child);

    if (visualParent != null)
        return GetHighestParent(visualParent);
    else
        return child;
}

then you might want to create a method to walkdown all the children like this:

void CallDelegateInAllControl2(DependencyObject parent)
{
    int childCount = VisualTreeHelper.GetChildrenCount(parent);

    for (int i = 0; i < childCount; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        if (child is UserControl2)
        {
            ((UserControl2)child).CallDelegate();
        }
        else
        {
            CallDelegateInAllControl2(child);
        }
    }
}

and then you call it within button1_click event

private void button1_Click(object sender, RoutedEventArgs e)
{
    DependencyObject parent = GetHighestParent(this);

    if(parent!=null)
        CallDelegateInAllControl2(parent);
}

note: a walk to get parent and child might be tricky and risky i think and i believe it's a long process so you might just want to re-layout your window1 so it has a StackPanel/Grid with a usercontrol1 element and all usercontrol2 elements within it so you can use the first code i post.

dnr3
@dnr3 Thanks, great
deep
no worries sir.
dnr3
@dnr3: When i mention this.Parent as StackPanel; it takes all the contorls in that usercontrol as childrens, but am using more that one usercontrol in my main form. how can i assign all the usercontrols in my mainWindow to the stackpanel children??????
deep
err that's why i said it's for your specific example. but if you want to assign all the usercontrols to be the stackpanel's children then you have to add the element into the stackpanel which contain your usercontrol1 or maybe you can make a recursive function to get the highest parent and walk down to their respective children/content
dnr3
ya i understand well now, i will try write a loop to get all the usercontrols to get mine. Sorry to ask u ah :| do u have any idea of getting the highest parent???
deep
you can try to mod the code here: http://www.hardcodet.net/2008/02/find-wpf-parent and make it recursive to get the parent
dnr3
ya i tried and got the topmost parent, but i cannot assign the UI elements to stackpanel as like StackPanel parentControl = this.Parent as StackPanel; this ?
deep
what do u mean by assigning? O_O. i was telling you to get the topmost parent is so you can walk down the children to get all the usercontrol2 type element. so the parent is not to be assigned to the stackpanel, i'll update my answer for an example
dnr3