WPF: view a list with multiple sources

Calling All WPF Experts!

I am converting an application I created to generate symbols for a specific RPG from Window Forms to WPF. For the most part, the program is much simpler in WPF form; however, I cannot imagine what this part is for my life.

I have a database that contains a list of skills that a character can adopt. This database contains no symbols - updating symbols will not change the database. The user can add new skills to the database if they want to, but this is separate from character creation. In the character creation window, I have a ListView to show the skills listed in the database. Clicking a row in the ListView refreshes the description text field and the Image control β€” all of which work great.

The character is stored in the character class; which contains a list of the skills that the character has adopted. This list does not contain all the skills in the database, so the user can exchange this character between applications with modified databases without issue.

I want to display some fields from a character class and some fields from a database in the same ListView. For example,

Name (database), Level (symbol), Score (symbol), MaxTakenCount (database)

What I want to achieve: The character's skill list will have zero skills until the user increases the "Level" value to a value greater than one. Changing "Level" will create an instance of this skill and save it in the character's skill list. The score is calculated using the formula stored in the database for that skill (same table as Name, MaxTakenCount). Setting "Level" back to zero removes the skill from the character's skill list. With any luck, the values ​​will be two-way, so I can use undo / redo. (The Undo queue stores serialized copies of the symbol object whenever a change is made.)

How feasible is this WPF? I don't know how to juggle a database string and a character's skill list to do what I want. Here's what I have so far. Note: Grade and Level bindings do not work as I do not know how to properly reference the skill list. Skill List is the data context of the tab that contains the ListView. ListView ItemSource is set to "SkillTable" behind the scenes.

 <ListView Name="listSkills" Margin="10" Grid.Row="2" IsSynchronizedWithCurrentItem="True" Background="#AAFFFFFF" FontSize="14">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name, Mode=TwoWay}" Margin="5" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Score">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Score, Mode=TwoWay}" Margin="5" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Level">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ListBox SelectedIndex="{Binding Level, Mode=TwoWay}">
                            <ListBox.Style>
                                <Style TargetType="ListBox">
                                    <Setter Property="ItemsPanel">
                                        <Setter.Value>
                                            <ItemsPanelTemplate>
                                                <StackPanel Orientation="Horizontal" Margin="5,0" HorizontalAlignment="Left" />
                                            </ItemsPanelTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </ListBox.Style>
                            <ListBoxItem Width="15"> </ListBoxItem>
                            <ListBoxItem Width="15">S</ListBoxItem>
                            <ListBoxItem Width="15">T</ListBoxItem>
                            <ListBoxItem Width="15">M</ListBoxItem>
                        </ListBox>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

      

The ListView is populated with every row in the database. The database columns are filled in correctly; however, character fields are not. Please help this WPF newbie !!

+2


source to share


1 answer


Try to look at it from a different perspective. A property ListView.ItemsSource

can bind to one collection item. So, first, create a class that encapsulates all the values ​​that you want to map to ListView

. Then create ObservableCollection<YourClass>

and populate all values ​​from the database, and other values ​​can be populated in code or later from the UI. Then bind that collection to a property ListView.ItemsSource

and everything works the way you want.



Oh ... just noticed the publication date. Ok, hope this helps someone else.

+1


source







All Articles