0

I have an issue with a custom event i have created. I have made a Usercontrol that looks the following:

    public partial class UCListView : UserControl {
    public UCListView() {
        InitializeComponent();
    }

    public event EventHandler SubmitClick;
    public event EventHandler MouseButtonUpEvent;
    private void SubmitButton_OnClick(object sender, RoutedEventArgs e) {
        if (SubmitClick != null)
            SubmitClick(this, e);
    }

    private void MouseButtonUp(object sender, RoutedEventArgs e) {
        if (MouseButtonUpEvent != null) {
            MouseButtonUpEvent(this, e);
        }
    }
}

Here is the MouseButtonUp event i have. The following is where i listen to the event:

    public partial class RoundsteelWindow : WindowControls {

    private UCListView uc;
    public RoundsteelWindow() {

        InitializeComponent();
        uc = new UCListView();
        uc.SubmitClick += new EventHandler(ButtonPressed);
        uc.MouseButtonUpEvent += new EventHandler(MousePressed);
        stkTest.Children.Add(uc);
        base.Test<RoundSteel>(uc, "Roundsteel");
    }
}

Here is the WindowControls, where the MousePressed method can be seen. This is the same as the code snippet beneath this code. Really don't see the issue:

    public abstract class WindowControls : Window {

    public IMaterialWith14Elements _ReturnObject { get; set; }
    public double amount { get; set; }
    private UCListView _uc;

    public void Test<T>(UCListView uc, string type) where T: IMaterialWith14Elements, new() {
        _uc = uc;
        List<T> test = MaterialLogic.GetList(type) as List<T>;
        foreach (T material in test) {
            uc.listView.Items.Add(material.Name);
        }
    }
    private string str;
    public void MousePressed(object sender, EventArgs eventArgs) {
        var item = (sender as ListView).SelectedItem;
        if (item != null) {
            _ReturnObject = _uc.listView.SelectedItems as FlatSteel ;
            str = item.ToString();
            _uc.amountText.IsEnabled = true;
        }
    }

    public void ButtonPressed(object sender, EventArgs e) {
        if (!string.IsNullOrEmpty(_uc.amountText.Text)) {
            amount = _uc.amountText.Text.customParseToDouble();
            this.Close();
        }
        else {
            MessageBox.Show("Indtast venligst en værdi.");
        }
    }
}

Now the problem is the following: With the following code it is working, but this class is not using the windowcontrols. It is called by another class which handles all of the buttons.

        private void flatsteelListView_PreviewMouseLeftButtonUp(object sender, RoutedEventArgs e) {
        var item = (sender as ListView).SelectedItem;
        if (item != null) {
            _returnObject = flatsteelListView.SelectedItems as FlatSteel;
            str = item.ToString();
            amountTextbox.IsEnabled = true;
            FindObject(str);
        }
    }

enter image description here enter image description here enter image description here

The first picture shows the working window. This is where there is not used a usercontrol. Actually this is a previous issue i have worked with and got help with here on stackoverflow Help for thisissue. The second picture is showing the next window using the usercontrol that has been created. The button event works and closes the window. Here comes then the issue, when the listview item is pressed. It is doing the same thing as on the first picture(where it works), but it is giving me a null reference, which doesn't make any sense to me. I have also checked the object sender to see if there was a difference between the sender of these two different windows.

I simply can't figure out why this is not working.

greetings darophi

Added with sender

Community
  • 1
  • 1
darophi
  • 456
  • 5
  • 15
  • Obviously your sender in the Screenshow isn't of type ListView. You could check this by seprating the conversion and then checking if the result is null. – Alexander Schmidt Nov 25 '15 at 13:08
  • 1
    I'll just leave [this](http://stackoverflow.com/q/4660142/1997232) here. – Sinatr Nov 25 '15 at 13:10
  • It would have been nice to see what `sender` is, you can almost see it but you covered it up by the exception window. The problem is the `as` cast is not working and returning null, you need to tell us what `sender` is when the exception happens. – Ron Beyer Nov 25 '15 at 13:21
  • I have added a picture where you can see sender – darophi Nov 25 '15 at 13:24

1 Answers1

0

Your sender is an object of UCListView class which is inherited from UserControl and you are trying to use it like ListView. So as result of operation (sender as ListView) you get null because sender is not an instance of ListView class and not inherits it.

melya
  • 578
  • 5
  • 24
  • I have made it work with this. Now it is the right type: this.uc.listView.PreviewMouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(this.MousePressed); – darophi Nov 25 '15 at 13:41
  • Yea, i goofed it. But now it works. Thx to everyone for their answers. – darophi Nov 25 '15 at 13:48