开发者

raising event in a usercontrol from another usercontrol

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;
        Message开发者_JAVA技巧Box.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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜