2

I have an application that can display items in two different ways, in rows using a StackPanel or icons using a WrapPanel. Which way it displays these items depends on a config setting. To populate these panels I have two seperate classes one inherits from the WrapPanel the other from the StackPanel. I was able to cut down on duplicated code using an Inferface. However I still had a lot of duplicated code the only difference between the code is the references to StackPanel or WrapPanel.

What I would really like to do is create a class that inherits from either the StackPanel or WrapPanel depending on the config setting.

public class ContainerBase : <wrap or stack>
{

//Do stuff!

}

Is this possible? Am I approaching this incorrectly?

5
  • 2
    This looks like workaround to me ... don't think that this is correct approach. How about using composition but not inheritance here? Commented Sep 6, 2012 at 14:58
  • How about using templates and styles? Commented Sep 6, 2012 at 15:13
  • 1
    @KarelFrajtak, templates and styles(+template selector) was first things that I thought about, but I don't have enough experience with them to be able to recommend smth useful in this case. But essentially yes - templates and styles sounds reasonable. Commented Sep 6, 2012 at 15:19
  • @Fred, I've added answer+code to illustrate what I meant in first comment Commented Sep 6, 2012 at 15:20
  • You definitely should learn how to use DataTemplateSelector and MVVM to work with WPF, it require much less code than what you are trying to do. Commented Sep 6, 2012 at 15:24

4 Answers 4

1

When I said "composition but not inheritance" in first comment, I meant smth like the following:

public class PanelPresentatinLogic
{
    public Panel Panel{get;set;}     

    public void DoSomeDuplicatingStuff()
    {
        //Do stuff! with Panel
    }    
}

public class SortOfStackPanel : StackPanel
{
    private readonly PanelPresentatinLogic _panelPresentatinLogic;

    public SortOfStackPanel(PanelPresentatinLogic presentationLogic)
    {
        _panelPresentatinLogic = presentationLogic;
        _panelPresentatinLogic.Panel = this;
    }

    public void DoSomeDuplicatingStuff()
    {
        _panelPresentatinLogic.DoSomeDuplicatingStuff();
    }
}


public class SortOfWrapPanel : WrapPanel
{
    private readonly PanelPresentatinLogic _panelPresentatinLogic;

    public SortOfWrapPanel(PanelPresentatinLogic presentationLogic)
    {
        _panelPresentatinLogic = presentationLogic;
        _panelPresentatinLogic.Panel = this;
    }

    public void DoSomeDuplicatingStuff()
    {
        _panelPresentatinLogic.DoSomeDuplicatingStuff();
    }
}

public class UsageSample
{
    public void PopulateCollectionOfItemsDependingOnConfigHopeYouveGotTheIdea()
    {
        string configValue = configuration["PanelKind"];
        PanelPresentatinLogic panelPresentatinLogic = new PanelPresentatinLogic();

        Panel panel = configValue == "Wrap" 
            ? new SortOfWrapPanel(panelPresentatinLogic)
            : new SortOfStackPanel(panelPresentatinLogic);
        // TODO: add panel to GUI
    }
}  
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Generics for this;

public class ContainerBase<T>  where T : Panel

You can then use the config setting to initialize the right type.

1 Comment

This assumes (possibly correctly) that the OP only needs methods from either class that are provided by Panel, or can refactor his code to make that happen.
1

In WPF you are intended to not use controls to populate itself. Instead, use MVVM pattern.

You can achieve your goal with only one class providing the data (tipically an ObservableCollection) and loading that "view mode variable" into a property at MVVM.

Then you can use a an ItemsPanel with an DataTemplateSelector to select the view.

Comments

0

May be you can use Panel?

    public class ContainerBase : <wrap or stack>
    {

    //Do stuff!

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.