Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I wrote a custom event in C sharp(Silverlight).Here is the code.
C#
public delegate void MyEvent(object sender, TreeGridEventArgs e);
        public event MyEvent SLTreeGridSelectionChanged;
        void RaiseSLTreeGridSelectionChanged(TreeGridEventArgs e)
        {
            if (SLTreeGridSelectionChanged != null)
            {
                SLTreeGridSelectionChanged(this, e);
            }
        }


and here I will invoke it( during the SelectionChanged event of DataGrid)
..

C#
private void SLDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
         
            if (CurrentSELedNode != null)
            {
                TreeGridRow row = (TreeGridRow)CurrentSELedNode.Tag;
                RaiseSLTreeGridSelectionChanged(new TreeGridEventArgs(row));
            }

        }


but while raising the event "SLTreeGridSelectionChanged" always shows null. Why this is happening? Any idea..? Please help me..
Posted
Comments
Ankit Rajput 23-Mar-11 3:14am    
Have you handled it in your code?

1 solution

The event is null as long as there are no subscribers. If you create an event handler and use the += syntax to subscribe for the event, it won't be null any more.
 
Share this answer
 
Comments
Arun India 23-Mar-11 4:23am    
But Kubajzz.. this code is working fine for me.. What I am missing..?

public delegate void EventHandler(object sender, TreeGridEventArgs e);
public event EventHandler TreeGridRatingChanged;
void RaiseSLTreeGridRatingChanged(TreeGridEventArgs e)
{
if (TreeGridRatingChanged != null)
{
TreeGridRatingChanged(this, e);
}
}

I am Raising it Here..

private void RatingItem_Click(object sender, RoutedEventArgs e)
{
RaiseSLTreeGridRatingChanged(new TreeGridEventArgs(treeGridRow));
}
Arun India 23-Mar-11 4:33am    
Hi Kubajzz..
Now It is working fine. Your answer is absolutely correct. After defining it, I missed to use it in the code where I include the dll.
Thank you Kubajzz for your reply..
Sergey Alexandrovich Kryukov 23-Mar-11 16:43pm    
Correct, a 5.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900