Click here to Skip to main content
12,815,019 members (40,678 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF
I have a WPF program in C#. In this program, I binded an object collection to tabControl:
<TabControl x:Name="tabControlMain" Margin="1,1,48,1" DataContext="{Binding}" Grid.RowSpan="2" Grid.ColumnSpan="2">
        <DataTemplate DataType="TabpageItem">
            <TextBlock Text="{Binding Title}">
                        <MenuItem Header="Remove" Click="MenuItem_Remove_Click">
                                <Image Width="20" Height="20"  Source="..\Resources\remove.png" Stretch="Fill" />
                        <MenuItem Header="Rename" >
                                <Image Height="20"  Source="..\Resources\rename.png" Stretch="UniformToFill" />
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Rename to:   " VerticalAlignment="Center"></TextBlock>
                                <TextBox Text="{Binding Title}" GotMouseCapture="TextBox_GotMouseCapture"></TextBox>
                    </ContextMenu >
    <TabItem Header="tabItem1" Name="tabItem1" >

and the code:
public MainWindow()
    tabControlMain.ItemsSource = totalInfoCollection.TabPageCollection;

Inside each tab there is a UserControl:
             d:DesignHeight="300" d:DesignWidth="300">
        <Grid Name="grid1">
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            <Grid Name="grid2">
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                <TextBlock Name="textBlock1" Text="Select by tags:" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10" />
                <TextBox Grid.Column="1" Text="{Binding SerachString,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"  Name="textBoxSelectionTags"  VerticalAlignment="Center" Margin="10" />
            <ListView DataContext="{Binding}" ItemsSource="{Binding InfoItemSerachResult,UpdateSourceTrigger=PropertyChanged}"  Grid.Row="1" Name="listView1" Margin="10">
                        <MenuItem Header="Add" >
                                <Image Height="20"  Source="..\Resources\add.png" Stretch="UniformToFill" />
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Add:   " VerticalAlignment="Center"></TextBlock>
                                <TextBox Name="TextBox_AddNewInfoName"  GotMouseCapture="TextBox_GotMouseCapture" MinWidth="100"></TextBox>
                                <Button Name="Button_AddNewInfo" Click="Button_AddNewInfo_Click" Content="Add"></Button>
                    </ContextMenu >

Inside the user control I have successfully written some codes for obtaining the binded object to the controls:
private void Button_AddNewInfo_Click(object sender, RoutedEventArgs e)
    TabpageItem t=(((((sender as Button).Parent as StackPanel).Parent as MenuItem).Parent as ContextMenu).PlacementTarget as ListView).DataContext as TabpageItem;
   // (sender as )

Now my problem is that inside the usercontrol constructor i dont know how to obtain the binded object to the usercontrol?
public partial class UserControlTabpageContent : UserControl
     public UserControlTabpageContent()
        // listView1.ItemsSource = ??????;
Posted 2-Jul-11 4:51am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

"this.DataContext as TabpageItem" should give you the "binded object" but DataContext won't be set yet in the constructor.

The object has to be created first before any binding occurs.

If the data you need to bind listView1.ItemsSource to is in the TabpageItem class, then you should use a Binding, which will bind when the DataContext is set.

I'm not sure why you keep trying to duplicate in code what you already have in XAML. Perhaps use one or the other? You already have a binding in XAML for the listview's ItemsSource. And you don't need DataContext="{Binding}" :)
aasser 5-Jul-11 4:26am
The problem is that this.DataContext is null.
If you need, I can send you the complete source code.
aasser 5-Jul-11 6:48am
My problem was solved by using Load event instead of using the constructor.
thank you.

private void UserControl_Loaded(object sender, RoutedEventArgs e)
listView1.ItemsSource = (listView1.DataContext as TabpageItem).InfoItemCollection;
Mark Salsbery 5-Jul-11 14:12pm
Yes, the data has to be there for the datacontext to be non-null :) Glad you got it working!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 2 Jul 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100