开发者

Data Binding with WPF

开发者 https://www.devze.com 2023-02-27 10:24 出处:网络
I\'m new to WPF and this following has stumped me for a while now: I have an observableCollection of People object in my model that is bound to my tabControl. So each my a new People object is added,

I'm new to WPF and this following has stumped me for a while now:

I have an observableCollection of People object in my model that is bound to my tabControl. So each my a new People object is added, a new tab is created with People.Title as the Tab's header.

Each People object has an ObservableCollection of Friend object. Inside of the Tab I would like to have a List of two textboxes, one for Friend.FirstName and another for Friend.LastName.

My first requirement is working fine, but the second one is giving me an error 'ItemsSource is already in use'

Here's my code so far:

<TabControl Name="ConversationTabs" Grid.Row="0" 
                ItemsSource="{Bindi开发者_开发百科ng}" 
                ItemTemplate="{StaticResource HeaderInfoTabControl}"
                ContentTemplate="{StaticResource DialogueList}" />

<Window.Resources>
    <DataTemplate x:Key="HeaderInfoTabControl">
        <TextBlock Text="{Binding Title}" />
    </DataTemplate>

    <DataTemplate x:Key="DialogueList">
        <ItemsControl ItemsSource="{Binding Path=DialogueCollectionVM}"> 
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Sent}" />
                <TextBlock Text="{Binding Path=DateSent}" />
                <TextBlock Text="{Binding Path=Message}" />
            </StackPanel>
        </ItemsControl>
    </DataTemplate>

</Window.Resources>

I appreciate your help.


You cannot add items to an ItemsControl and use the automatic population (via ItemsSource) at the same time. If that StackPanel is supposed to be used for the items in the ItemsSource you should do this:

<ItemsControl ItemsSource="{Binding Path=DialogueCollectionVM}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Sent}" />
                <TextBlock Text="{Binding Path=DateSent}" />
                <TextBlock Text="{Binding Path=Message}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
0

精彩评论

暂无评论...
验证码 换一张
取 消