sats
sats

Reputation: 159

Handling events across different classes using C#

I have a property defined in Class A. When the property is changed then i need to raise an event. Another class B should respond to this event and do something. I have done something like this:

Code:

Class A{
string variable = "test";
public delegate void EventHandler(object sender, EventArgs args);
public static event EventHandler ThrowAddEvent = delegate { };
public static event EventHandler ThrowRemoveEvent = delegate { };
public String Name { get; set; }
public int Select { get; set; }
public Window Formref1 { get; set; } 
 public bool IsSelected
    {
        get { return IsSlctd; }
        set
        {
            IsSlctd = value;
            if (value)
            {
                ThrowAddEvent(this, new EventArgs());

            }

            else
            {
                ThrowRemoveEvent(this, new EventArgs());
            }


        }
    }
}

Another class which is responding to this event is defined as follows:

Class B{
 public B(ParsedResult _result)
    {
        InitializeComponent();
        if (_result != null)
        {
            this.Result = _result;
            PopulateColumns1();
            DataGrid1.Items.Clear();
            DataGrid1.ItemsSource = Populatevariables();

        }
    }
 public void PopulateColumns1()
    {
        DataGridTextColumn Colvar = new DataGridTextColumn();
        Colvar.Header = "Variables";
        Colvar.Binding = new Binding("Name");
        DataGrid1.Columns.Add(Colvar);

         DataGridTemplateColumn Col = new DataGridTemplateColumn();


        Col.Header = "Select";

        DataTemplate dd = new DataTemplate();
        FrameworkElementFactory FF = new FrameworkElementFactory(typeof(CheckBox));
        FF.SetBinding(CheckBox.BindingGroupProperty, new Binding("Select"));
        FF.SetBinding(CheckBox.IsCheckedProperty, new Binding("IsSelected") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
        FF.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center);


        dd.VisualTree = FF;
        Col.CellTemplate = dd;

        DataGrid1.Columns.Add(Col);
    }

private List<A> PopulateVariables()
    {

        CheckBox cb = new CheckBox();
        List<A> CharList = new List<A>();
        Result.GetCharacteristicList.MoveNext();
        IEnumerator<A2LCharacteristic> enumeratorlist = Result.GetCharacteristicList;
        for (int i = 0; enumeratorlist.MoveNext(); i++)
        {
            CharList.Add(new A() { Name = enumeratorlist.Current.Name,  Select = i, Formref1 = this});

        }


        enumeratorlist.Reset();
        return CharList;
    }
private void OKBtn_Click(object sender, RoutedEventArgs e)
    {            
        A Instance_A = new A();

        Instance_A.ThrowAddEvent += (sender1, args) => { Addvrbl(Instance_A.variable); };

        Instance_A.ThrowRemoveEvent += (sender2, args) => { RmvVrbl(Instance_A.variable); };
        this.Close();
    }

 public void Addvrbl(string vrbl)
    {
        if (!(vrbllist.Contains(vrbl)))
        {
            vrbllist.Add(vrbl);
        }
    }

    public void RmvVrbl(string vrbl)
    {
        if ((vrbllist.Contains(vrbl)))
        {
            vrbllist.Remove(vrbl);
        }
    }
}

The problem is it is not going inside the method "AddVrbl" and "RmvVrbl". I have used the solution from here. Please help.

Upvotes: 0

Views: 152

Answers (1)

Bolu
Bolu

Reputation: 8786

OK, instead of subscribing to the event of a new instance of A,which will never get triggered/published. When you initializing CharList, you have to subscribe to the event of each A item.

Something like:

for (int i = 0; enumeratorlist.MoveNext(); i++)
{
     var obja=new A() { Name = enumeratorlist.Current.Name, Select = i, Formref1 = this};
     obja.ThrowAddEvent += ...
     obja.ThrowRemoveEvent += ...
     CharList.Add(obja);    
}

PS: Make sure you un-subscribe these events before removing an item from your CharList.

Upvotes: 1

Related Questions