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


and the code:
C#
public MainWindow()
{
    InitializeComponent();
    //////////////////////////////////////////////////////////////////////////
    Load();
    tabControlMain.Items.Clear();
    tabControlMain.ItemsSource = totalInfoCollection.TabPageCollection;
}



Inside each tab there is a UserControl:
XML
<UserControl
             x:Class="InfoManager.Presentation_Layer.UserControlTabpageContent"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid Name="grid1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Name="grid2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <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" />
            </Grid>
            <ListView DataContext="{Binding}" ItemsSource="{Binding InfoItemSerachResult,UpdateSourceTrigger=PropertyChanged}"  Grid.Row="1" Name="listView1" Margin="10">
                <ListView.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Add" >
                            <MenuItem.Icon>
                                <Image Height="20"  Source="..\Resources\add.png" Stretch="UniformToFill" />
                            </MenuItem.Icon>
                            <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>
                            </StackPanel>
                        </MenuItem>
                    </ContextMenu >
                </ListView.ContextMenu>
            </ListView>
        </Grid>
    </Grid>
</UserControl>




Inside the user control I have successfully written some codes for obtaining the binded object to the controls:
C#
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;
    //t.
   // (sender as )
}


Now my problem is that inside the usercontrol constructor i dont know how to obtain the binded object to the usercontrol?
C#
public partial class UserControlTabpageContent : UserControl
 {
     public UserControlTabpageContent()
     {
         InitializeComponent();
         listView1.Items.Clear();
        // listView1.ItemsSource = ??????;
     }
Posted

1 solution

"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}" :)
 
Share this answer
 
v4
Comments
[no name] 5-Jul-11 4:26am    
The problem is that this.DataContext is null.
If you need, I can send you the complete source code.
[no name] 5-Jul-11 6:48am    
My problem was solved by using Load event instead of using the constructor.
anyway.
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900