开发者

WPF how to bind a tabitem with a list

开发者 https://www.devze.com 2023-02-19 03:27 出处:网络
I have the following problem: i have some data loaded in my application, that need to be put in a tab control.

I have the following problem:

i have some data loaded in my application, that need to be put in a tab control.

The data is in the format:

class objectType1
{
    string property_1;
    string prorerty_2;
}

class mainObject
{
    string mainProperty_1;
    string mainProperty_2;
    List<objectType1> objectsList;
}

and all the data is loaded in an object of type

List<mainObject> myListofObjects

So far i managed to create the tabitems with respect to myListofObjects item (ie if the list has 5 objects, 5 tabs are created with the header containing the information mainProperty_1 and mainProperty_2) now i need to add the data contained in each objectsList into their respective tab...

the mainProperty_1 represents an image, which must be loaded...

<TabControl x:Name="_DataList" Margin="10">
        <!-- Header -->
        <TabControl.ItemTemplate>
            <DataTemplate>
                <S开发者_开发百科tackPanel Orientation="Horizontal">
                    <Image Height="18" Source="{Binding mainProperty_1/>
                    <TextBlock Text="{Binding mainProperty_2}" Margin="2,0,0,0" />
                </StackPanel>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <!-- Content -->
        <TabControl.ContentTemplate>
            <DataTemplate x:Name="objectDataTemplate">
                <Grid Margin="5">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding property_1}" ToolTip="{Binding property_2}" IsHitTestVisible="false" Stretch="Uniform"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

I pass the data to the tabcontrol in code behind with

_DataList.ItemsSource = myListofObjects;

this is not working for the content...

the header loads just fine (both image and the text...)

anyone has any idea how to do it?

Thanks a lot!


your ContentTemplate seems to be wrong:

 <TabControl x:Name="_DataList" Margin="10">
    <!-- Header -->
    <TabControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Height="18" Source="{Binding mainProperty_1/>
                <TextBlock Text="{Binding mainProperty_2}" Margin="2,0,0,0" />
            </StackPanel>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <!-- Content -->
    <TabControl.ContentTemplate><!-- its bound to one mainObject -->
        <DataTemplate x:Name="objectDataTemplate">
 <!-- if you wanna bind to something from your objectsList you have to threat it like a list, cause it is :) -->

<ListBox Itemssource={Binding objectsList}>
<ListBox.ItemTemplate>
    <DataTemplate DataType="{x:Type objectType1}">
       <StackPanel Orientation="Horizontal">
           <Image Source="{Binding property_1}" ToolTip="{Binding property_2}" IsHitTestVisible="false" Stretch="Uniform"/>
       </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>


1) Have you tried to connect to list via binding?

var b = new Binding("myListofObjects");
BindingOperations.SetBinding(_DataList, ItemsControl.ItemsSourceProperty, b);

2) If you're using binding, have you set appropriate DataContext?

0

精彩评论

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