Home > Enterprise >  Datatemplate binding itemsource inside Datatemplate
Datatemplate binding itemsource inside Datatemplate

Time:10-21

I want to bind a ObservableCollection to a Itemscontrol inside a Datatemplate, that is inside another Datatemplate:

<ListView x:Name="list_befehlsfolge" Margin="5">
    <ListView.ItemTemplate>
        <DataTemplate DataType="{x:Type local:Befehlszeile}" >
            <Expander Margin="5" Header="{Binding Path=Bezeichnung,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                <ItemsControl ItemsSource="{Binding Path=SubBefehlsliste}">
                    <DataTemplate DataType="{x:Type local:SubZeile_Text}">
                        <TextBox Text="{Binding Path=text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
                    </DataTemplate>
                </ItemsControl>
            </Expander>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Binding the source to list_befehlsfolge.Itemsource with code behind is no problem, but how can I bind SubBefehlsliste?

When I create a new instance of SubBefehlsliste like

public class Befehlszeile : Position
{
    public string Bezeichnung { get; set; } = "Befehlsname";
 
    // crash at this line:  
    public ObservableCollection<Position> SubBefehlsliste { get; set; } = new ObservableCollection<Position>();
   
    public Befehlszeile()
    {
        //  SubBefehlsliste.Add(new SubZeile_Text());
    }
}

it crashes with an error

InvalidOperationException: The operation is invalid while using 'ItemsSource'. Instead, use ItemsControl.ItemsSource to access and modify items. (translated with google)

(Position is a "Mother"-class for all Datatypes like SubZeile_Text and other, to add all to a ObservableCollection)

CodePudding user response:

You forgot to specify a property for the template. So the Template you specified was an attempt to add to the ItemsControl.Items collection.
But you cannot use ItemsSource and Items at the same time.

Another incomprehensible moment: in the Sharpe code you use the Position class for the elements of the collection, and in the Data Template you set the local:SubZeile_Text type.
Which of these options is correct?

<ListView x:Name="list_befehlsfolge" Margin="5">
    <ListView.ItemTemplate>
        <DataTemplate DataType="{x:Type local:Befehlszeile}" >
            <Expander Margin="5" Header="{Binding Path=Bezeichnung,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                <ItemsControl ItemsSource="{Binding Path=SubBefehlsliste}">
                    <!--Forgotten this line below-->
                    <ItemsControl.ItemTemplate>
                        <DataTemplate DataType="{x:Type local:Position}">
                            <TextBox Text="{Binding Path=text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Expander>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

P.S. Tip: If you don't define columns, then use a ListBox, not a ListView.

  • Related