0

So i was wondering how i would make some kind of superwindow/superclass for my windows. The windows look the following:

Window with functionality Buttons for the windows

The first picture shows how every single one of the windows would potentially look when pressed on one of the buttons. So each of these windows would have the same basic functionality but with some different returning objects which can be seen in the following code.

public partial class FlatsteelWindow : Window {

    private string str;
    private IList test;

    public FlatSteel _returnObject { get; set; }
    public double amount { get; set; }

    public FlatsteelWindow(object inputObject) {
        InitializeComponent();
        _returnObject = inputObject as FlatSteel;
        FetchList();
    }

    private void FetchList() {
        test = MaterialLogic.GetFlatsteelList() as List<FlatSteel>;
        foreach (FlatSteel flatSteel in test) {
            flatsteelListView.Items.Add(flatSteel.Name);
        }
    }

    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);
        }
    }

    private void FindObject(string s) {
        foreach (FlatSteel flatSteel in test) {
            if (s.Equals(flatSteel.Name)) {
                _returnObject = flatSteel;   
            }
        }
    }

    private void submitButton_Click(object sender, RoutedEventArgs e) {
        if (!string.IsNullOrEmpty(amountTextbox.Text)) {
            amount = amountTextbox.Text.customParseToDouble();
            this.Close();
        }
        else {
            MessageBox.Show("Indtast venligst en værdi.");
        }
    }
}

This is the code for the shown Flatsteel window. Here is the code which creates the window:

        private void flatsteelButton_Click(object sender, RoutedEventArgs e) {
        FlatsteelWindow flatsteelWindow = new FlatsteelWindow(this);
        flatsteelWindow.ShowDialog();
        test = flatsteelWindow._returnObject;
        amount = flatsteelWindow.amount;
        if (test != null && amount != null) {
            AddToGridView();
        } 
    }

    public FlatSteel test { get; set; }
    private double amount { get; set; }

As we can see in this code the flatsteel object is needed where this method is invoked. The 6 different windows, which each are represented by the six buttons on the 2nd picture, each return an object to their type of material. So to make it clear what my question is: How do i make one superclass/superwindow that has all the same basic funtionality, from which i then can inherit from and add the needed functionality for the different returning objects?

Spawn
  • 935
  • 1
  • 13
  • 32
darophi
  • 456
  • 5
  • 15
  • Why it can't be usercontrol? – Spawn Nov 24 '15 at 20:02
  • Please make it clear what do you mean by "superwindow"? You want to create a program with docking subwindows, like the UI of visual studio? – Gergely Hamos Nov 24 '15 at 20:04
  • Delete all that and use proper DataBinding. That code doesn't belong in code behind, it belongs in a proper data model or ViewModel. – Federico Berasategui Nov 24 '15 at 20:13
  • I think I agree with Highcore that the way to approach this would be to separate it so your data object is separate from your UI. So you'd only have one copy of your UI (Window or UserControl... personally [I prefer a UserControl](http://stackoverflow.com/a/12216068/302677)) that is bound to your data object, and that data object could be a Flatsteel, RoundSteel, or whatever else. The UI wouldn't care which as long as it shared the same base interface. If that really doesn't work for you, you could also use type generics (`MyWindow : Window` where `T` is FlatSteel or `IBaseMaterial`) – Rachel Nov 24 '15 at 20:51
  • Thanks for the great feedback. I will try to work with this when i wake up tomorrow and write my feedback here as soon as i got some. – darophi Nov 24 '15 at 21:11

0 Answers0