Avatar billede xenoxsis Nybegynder
22. juni 2011 - 20:14 Der er 4 kommentarer

WPF: Binde items control inde i items control.

Hej Eksperter,

Jeg skal forsøge at lave et budget program i WPF. Og vil lave grupper af poster. Dvs. man skal kunne oprette en gruppe (GroupBox) som indeholder nogle linier med beløb. F.eks.:

|--- Bolig ---------------------|
| Husleje                  5000 |
| Opsparing (Reperationer)  500 |
| Osv...                        |
|-------------------------------|

Jeg overvejede om man kunne lave et items control hvor datatemplate var en Groupbox og inde i groupbox'en skulle jeg så have endnu en itemscontrol som bindede til de linier man så skulle bruge. Men ved ikke helt om det er den smarteste måde, og desuden ved jeg ikke hvordan det kan lade sig gøre. Er der måske nogle der har nogle pointers?
Avatar billede xenoxsis Nybegynder
22. juni 2011 - 20:18 #1
Eksempel:

<ItemsControl DataContext="{Binding}">
    <DataTemplate>
        <GroupBox Header="{Binding GroupName}">
            <ItemsControl DataContext="{Binding}">
                <DataTemplate>
                    <TextBlock Text="{Binding Text}"></TextBlock>
                    <TextBlock Text="{Binding Amount}"></TextBlock>
                </DataTemplate>
            </ItemsControl>
        </GroupBox>
    </DataTemplate>
</ItemsControl>

Men kan man binde det i C# på den måde? Og hvordan?
Avatar billede xenoxsis Nybegynder
22. juni 2011 - 20:24 #2
Jeg mangler naturligvis lige <ItemsControl.ItemTemplate> inden <DataTemplate>
Avatar billede tjens Nybegynder
23. juni 2011 - 21:00 #3
XAML:
<Window x:Class="WpfCollectionX2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <DataTemplate x:Key="Lines">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Description}" Width="120" ></TextBlock>
                <TextBlock Text="{Binding Amount}"  TextAlignment="Right" Width="120" ></TextBlock>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="Groups">
                <GroupBox Header="{Binding GroupName}">
                    <ItemsControl ItemsSource="{Binding BudgetLines}" ItemTemplate="{StaticResource Lines}">
                    </ItemsControl>
                </GroupBox>
        </DataTemplate>
    </Window.Resources>

    <Grid>
            <ItemsControl ItemsSource="{Binding BudgetGroups}" ItemTemplate="{StaticResource Groups}">
            </ItemsControl>
    </Grid>
</Window>


CS:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace WpfCollectionX2
{
    public partial class Window1 : Window
    {
        ObservableCollection<BudgetGroup> budgetGroups = new ObservableCollection<BudgetGroup>();
        public ObservableCollection<BudgetGroup> BudgetGroups { get { return budgetGroups; } }

        public Window1()
        {
            budgetGroups.Add(new BudgetGroup() { GroupName = "Test01" });
            budgetGroups[0].addBudgetLine(new BudgetLine() { Description = "Line 01 01", Amount = 101m });
            budgetGroups[0].addBudgetLine(new BudgetLine() { Description = "Line 01 02", Amount = 102m });
            budgetGroups.Add(new BudgetGroup() { GroupName = "Test02" });
            budgetGroups[1].addBudgetLine(new BudgetLine() { Description = "Line 02 01", Amount = 201m });
            budgetGroups[1].addBudgetLine(new BudgetLine() { Description = "Line 02 02", Amount = 202m });
            budgetGroups[1].addBudgetLine(new BudgetLine() { Description = "Line 02 03", Amount = 203m });

            InitializeComponent();
        }
    }

    public sealed class BudgetGroup
    {
        ObservableCollection<BudgetLine> budgetLines = new ObservableCollection<BudgetLine>();

        public string GroupName { get; set; }
        public ObservableCollection<BudgetLine> BudgetLines { get { return budgetLines; } }
       
        public void addBudgetLine(BudgetLine line) {
            this.budgetLines.Add(line);
        }
    }
    public sealed class BudgetLine
    {
        public string Description { get; set; }
        public decimal Amount { get; set; }
    }

}


Da jeg er nybegynder indenfor WPF og C#, vil jeg meget gerne have kritik og gode råd :-)
Avatar billede tjens Nybegynder
23. juni 2011 - 21:15 #4
XAML'en kunne også skrives uden brug af staticresource:
        <ItemsControl ItemsSource="{Binding BudgetGroups}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <GroupBox Header="{Binding GroupName}">
                        <ItemsControl ItemsSource="{Binding BudgetLines}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Description}" Width="120" ></TextBlock>
                                        <TextBlock Text="{Binding Amount}"  TextAlignment="Right" Width="120" ></TextBlock>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </GroupBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester