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;
}
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();
}