WPF / MVVM Question - Iterate, add and modify collection

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1842 Views - Last Post: 06 May 2015 - 08:29 AM Rate Topic: -----

#1 Haegr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 13-May 14

WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 02:09 PM

I'm not sure if this is the right place or not but...here goes.

I am not sure on the exact terms or I could google this stuff myself (never used WPF / MVVM before other than a few basic data binding tutorials).

I am looking to create an "editor" of sorts for items in a game I am building with a friend. It will either be stored in a database or a XML file. Each "item" will have a name, a type (text), various stats (health, mana) and a description.

I need to be able to go "next" and "previous" on all saved items and perhaps filter based on type / name.

Any suggestions? Help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: WPF / MVVM Question - Iterate, add and modify collection

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 03:12 PM

Suggestions for... What? What the UI should look like? What database to use? No idea what you are looking for from us.
Was This Post Helpful? 0
  • +
  • -

#3 Haegr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 13-May 14

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 03:14 PM

I've never used WPF or MVVM to this extent, I'm looking for suggestions on how to structure it / how to bind it.

If possible the actual technical terms of what I am trying to accomplish so I can google it to find documentation on it. I am sure what I want is possible I just do not know how to go about doing it.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 03:21 PM

The technical term is "binding". But you've said that already so you know the term.

When you say "I know what I want to do but not how to do it" - what is you want to do, that you haven't already described?

It really sounds like you're asking "Would someone show me how to architect my program?"

I think you're just afraid to get started, or afraid to make a mistake. Don't be. We learn more from mistakes that successes.

You described your Item class. Start by making that.

Are you going to code the database interaction yourself, or are you going to use an OR/M such as Entity Framework or LLBLgen? There is a lot to be said for having the DAL created for you. But there is a lot to be learned if you code it all yourself.
Was This Post Helpful? 0
  • +
  • -

#5 Haegr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 13-May 14

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 03:41 PM

Sorry if it came across that way, let me try to be more specific with my question.

I am comfortable with creating a binding for the object, which I have already done (for a separate project but creating one for this project should not be an issue). Right now I am trying to figure out how to actually deal with the editing / moving to the next item.

The only type of binding I have done is single binding on items, I've never created something that goes to another item. Is it as simple as just increasing the index I am looking at to find the next item in the list / database? This is the main issue I think.

I have as I have never used an ORM systems. Would a database be best suited to this? Or an XML? I am leaning towards XML but I am not sure if that will net the best results / change anything in regards to the work at all.

Also sorry if I used the correct term (it was unknowingly), I thought that binding within a database / cycling over a collection might have a specific term.

To answer your question I guess I am just looking for advice / direction to documentation / tutorials on similar things. I'm not looking for someone to do the work for me I'm just trying to understand what tools / techniques I actually need to use.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,311
  • Joined: 12-December 12

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 04:06 PM

Quote

The only type of binding I have done is single binding on items, I've never created something that goes to another item.

As a stepping stone you could build your Item class, implementing INotifyPropertyChanged so that changes to the data are reflected in the UI.

Then you would construct an ObservableCollection of your Class and bind some list-element to this collection. Buttons could then use Command objects to move next and previous through the collection.

This is only a stepping stone. You then might study the MMVM pattern to structure the interaction between your data-store and the UI correctly. It is at this point that you may also want to investigate ORMs.

Whether to work with a database or XML is up-to you. XML is a text, insecure, format, more suited to simple data-structures on a per-user basis. If you need secure, multi-user/ shared/ concurrent data access then you need a database.



Next and previous buttons, and using XML or a database, are not the main issues. Knowing how to structure, and separate, your data from your UI is.

This post has been edited by andrewsw: 22 April 2015 - 04:17 PM

Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 04:08 PM

Personally I would go to a database.
An ORM is an Object-Relational Mapper. It creates objects for you to work with and does the mapping to the relational database. So you code to the classes as you always would normally, and the Data Access Layer is handled for your by the ORM. Entity Framework is probably the most common. You should read up on it and do some of the familiarization tutorials so you can get a feel for what it does.

Binding to collections is nearly as straightforward as binding to single values that you've done. If you use an ObservableCollection<T> you can just iterate through the collection. It will also raise an INotifyPropertyChanged event when an item in the collection changes: Hence Observable. So if you have that item on screen and something changes it it the GUI will update for you.

Two suggetions:
  • Don't refer to your things as 'items'. All the collection GUI elements already use this term and it will get confusing quickly. For example a ListView has a collection of Items, an ItemsSource and so on. FYI: Its the ItemsSource property of a listview that you could bbind to your ObservableCollection<Thingies> if you wanted to present them in a list then have the user select one.
  • Don't use a string (text) for the type of the thingie. It leads to mistakes when you try to say its an "Axe" or an "Ax" or an "ax" or an "axe" in code. Use enums.

This post has been edited by tlhIn`toq: 22 April 2015 - 04:10 PM

Was This Post Helpful? 2
  • +
  • -

#8 Haegr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 13-May 14

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 04:09 PM

ObservableCollection seems to be what I was looking for!

It will only ever be accessed by a single user so XML should do the trick for now (it is only an in house tool).

Appreciate the help.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,311
  • Joined: 12-December 12

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 04:15 PM

There are also WPF tutorials here at DIC, click the Tutorials link at the top of this page.
Was This Post Helpful? 1
  • +
  • -

#10 Haegr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 13-May 14

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 04:18 PM

I have heard of Entity Framework and my plan for this weekend was to toy with it. I'll add the ObservableCollection stuff to there too.

As for your suggestions.
1) You are referring simply to the name of my model? If so, not to worry as they will be named "Materials" and other such things, items just seemed a generic term to use for this example to try to keep it generic.
2) I am not sure I follow what you mean to not use strings, you mean not to use strings as part of the class but to use ENUMs instead?

Appreciate the direction to the tutorials but it's difficult to know which tutorial to do / look for when you don't know the terms, hence this question. Now that I know the term it is a lot easier to find what I am looking for.
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 04:23 PM

There's a list of WPF tutorials in my signature block. They should get you started and each of those has some other tutorials referenced.

Quote

Each "item" will have a name, a type (text),

This is what I was referring to. Don't use text to indicate the type. Use an enum:


enum MaterialTypes
{
    Concrete,
    Leather,
    Wood,
    Cotton,
    Linen,
    Steel
}

// instead of

myWidget.Type = "Cotton"; // Specifying the type by text is bad

Was This Post Helpful? 1
  • +
  • -

#12 Haegr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 13-May 14

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 22 April 2015 - 04:28 PM

Oh, yes I see now. How would this then look in a database? Would I be storing the integer for the ENUM or the text?

Is the only reason text is bad because of possible programmer errors? Or is it because the ENUM can be used to create a combo box type input?
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 23 April 2015 - 04:36 AM

I store the text of the enum in the database, because if someone reorganizes the order in the C# the values won't match up. So you store "Steel" and when you read it back you find the enum value that matches.

There's a lot of reasons to use enums, but the biggest one is lack of confusion. Also that Visual Studio will help you find what you want.

As you start typing myNamespace.myEnum. at this point Visual Studio's Intellisense will give you a pop-up menu of the enum values.

switch constructs are a lot easier

switch (mySelectedMaterial)
{
    case Materials.Steel:
      // do this
      break;
    case Materials.Cotton:
      // do this
      break;


When you use strings, and then you make updates/changes you have to spend hours scrubbing through all your code to update the 500 places you used the string "Cotton". But you can rework the enum all you like and not break any of the references.

This post has been edited by tlhIn`toq: 23 April 2015 - 09:21 AM

Was This Post Helpful? 2
  • +
  • -

#14 Haegr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 13-May 14

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 04 May 2015 - 04:12 PM

I am just wondering. When I am creating a property, example:

        public String Description
        {
            get { return description; }
            set
            {
                description = value;
                onpropertychanged("DESCRIPTION");
            }
        }



Would it be best to change the "DESCRIPTION" text to an enum? And the same for all other properties?

And one other question.

While my ObservableCollection does indeed update with SelectedElements, and I am able to edit the values in a single TextBox I am unable to edit values which are nested within a wrappanel. Code example below:

<window.Resources>
  <Style x:Key="BasicLabel" TargetType="{x:Type Control}">
    <Setter Property="Width" Value="100"/>
    <Setter Property="Height" Value="25"/>
  </Style>
</window.Resources>

<Grid>
//Row and column definitions
<TextBox Grid.Row="0" Grid.Column="1" Text ="{Binding SelectedMaterial.Description, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  Width="100" /> [b]//This one works[/b]
<WrapPanel Grid.Row="2" Grid.Column="0">
  <WrapPanel.Resources>
    <Style BasedOn="{StaticResource BasicLabel}" TargetType="{x:Type Label}"/>
    <Style BasedOn="{StaticResource BasicLabel}" TargetType="{x:Type TextBox}"/>
  </WrapPanel.Resources>
  <TextBox Text="{Binding SelectedMaterial.Description, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBox> [b]//Does not work[/b]
</Grid>



Forgot to add on the one above (and I have no idea how to edit so I have to post again) that the second textbox does show the data but is not clickable / accessible.
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WPF / MVVM Question - Iterate, add and modify collection

Posted 05 May 2015 - 03:51 AM

First - This code doesn't work, does it?

Quote

public String Description
{
    get { return description; }
    set
    {
        description = value;
        onpropertychanged("DESCRIPTION");
    }
}
Your property name is "Description" but you're raising a notification that "DESCRIPTION" has changed. C# is a case-sensitive language.

Quote

Would it be best to change the "DESCRIPTION" text to an enum? And the same for all other properties?
Are you saying you want to do this:
onpropertychanged(MYPROPERTIES.DESCRIPTION); because if you are I want you to try it and tell me if its even legal in C#.

Quote

While my ObservableCollection does indeed update with SelectedElements, and I am able to edit the values in a single TextBox I am unable to edit values which are nested within a wrappanel.
That has nothing to do with it being in a wrap panel and everything to do with the fact that you are using the wrong case for "Description" as mentioned above.

Two tutorials for you, both are linked in my signature block. One is the tutorial on properties. The other is a the link for "A pair of C# property snippets."

This post has been edited by tlhIn`toq: 05 May 2015 - 03:54 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2