0

I have this Usercontrol with a Listview loaded in the Mainwindow:

 <Controls:MetroAnimatedSingleRowTabControl  Grid.Row="1" x:Name="MainTabControl" Controls:TabControlHelper.IsUnderlined="True" Margin="10,0,0,1">
            <TabItem Controls:ControlsHelper.HeaderFontSize="40" Header=" List"  Foreground="#CCB5BABB" Controls:ControlsHelper.HeaderFontStretch="UltraExpanded" HorizontalAlignment="Left" VerticalAlignment="Top" >
                <load:Usercontrol1 DataContext="{Binding}"  />
            </TabItem>

From this Usercontrol a ButtonClick calls another form for entering new data. After saving the data to database, I call a method loading the list in Usercontrol by referencing the entire Usercontrol to the entry window :

 private readonly Usercontrol1 temp;
    public newDataEntry(Usercontrol1 temp2)
    {
        InitializeComponent();
        temp= temp2;
    } 
     private void buttonentry(object sender, RoutedEventArgs e)
    {
        temp.fillList(); // list in Usercontrol fill
        this.Close();
    }

Since I want to use the same entry form with different Usercontrols, is there a more effective way to call method in Usercontrol?

Beldrak
  • 27
  • 1
  • 5

1 Answers1

0

Without a good Minimal, Complete, and Verifiable example that shows clearly what you are doing, why you want to call this method, what the method does, and what specific problem you are having generalizing the action, it is impossible to know for sure what the best answer for your scenario is. That said, some discussion can be provided.

First and foremost, it is a mistake for your newDataEntry class to depend on the Usercontrol1 class at all. This should already be apparent, due to the issue you are running into trying to reuse it with other UserControl classes, but it is also a basic OOP concept: a class that exists to support some other class should not itself carry a dependency on that other class. Doing so breaks reusability in a way that is fundamentally opposite a primary goal of OOP.

So how do you get rid of this dependency? Well, the most general way in C# would be for your Usercontrol1 to subscribe to the newDataEntry object's Closed event. Then it can do whatever it wants at that time, including calling its own fillList() event.

Of course, if the newDataEnty window is used modally (i.e. you call ShowDialog()), then subscribing to the Closed event is overkill. You can just call whatever code you need to when the ShowDialog() method returns.

All that said, the name fillList() hints that you're copying list data directly into some list-based control (e.g. the ListView you mentioned). When in fact, in a WPF program, you should be manipulating only view models and letting the UI respond accordingly. Again, without a good MCVE showing context, it's impossible to say for sure that's what you're doing, never mind provide any specific advice along those lines. Suffice to say, it's likely that this code doesn't belong in the Usercontrol1 class at all.

See also these related posts:

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Appreciatte your wholesome answer. To make things more clear, the fillList() method connects to a dabatase and fills a Listview with data displayed/used only in the Usercontrol1 itself. newDataEntry form called with .Show(); method buttonentry simply adds new entries to database, "refreshes" the Usercontrol1's Listview and closes the entry form. Since I'm a scholar with 2 weeks of C#/WPF exp., I don't really understand more difficult techniques. It would be really appreciated if you could tell (according to the given info) what would be the most general way to call the fillList(). – Beldrak May 01 '16 at 10:59
  • The only information you've added is in your comment, and you haven't even clarified whether the `newDataEntry` window is shown modally or not. In any case, I did state above the two most likely correct approaches: if modal, then just have the _caller_ call the `fillList()` method when `ShowDialog()` returns; if not modal, then subscribe to the `Closed` event and have the event handler call `fillList()`. Either way, `newDataEntry` has no reason to know anything about any user control objects. – Peter Duniho May 01 '16 at 15:52