Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
Hi
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">
    <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:
public MainWindow()
{
    InitializeComponent();
    //////////////////////////////////////////////////////////////////////////
    Load();
    tabControlMain.Items.Clear();
    tabControlMain.ItemsSource = totalInfoCollection.TabPageCollection;
}
 

Inside each tab there is a UserControl:
<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:
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?
public partial class UserControlTabpageContent : UserControl
 {
     public UserControlTabpageContent()
     {
         InitializeComponent();
         listView1.Items.Clear();
        // listView1.ItemsSource = ??????;
     }
Posted 2-Jul-11 3:51am

1 solution

Rate this: bad
good
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}" Smile | :)
  Permalink  
v4
Comments
aasser at 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 at 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 at 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
0 Sergey Alexandrovich Kryukov 528
1 OriginalGriff 459
2 ChintanShukla 305
3 Richard Deeming 275
4 RyanDev 230
0 Sergey Alexandrovich Kryukov 8,858
1 OriginalGriff 7,451
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 2 Jul 2011
Copyright © CodeProject, 1999-2014
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