Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi To make a long story short, I cannot bind a textbox correctly.

This is photo of my program:
The Link[^]

I have a tabControl. Inside the tabControl, there are several tabs that are defined by a template:
XML
<TabControl.ContentTemplate>
    <DataTemplate>
         <myUserControl:UserControlTabpageContent>
         </myUserControl:UserControlTabpageContent>
    </DataTemplate>
</TabControl.ContentTemplate>


They contain a user control.
The tab controller is binded to a collection of this class:
XML
[Serializable]
public class TabpageItem
{
    public string Title { get; set; }
    private ObservableCollection<InfoItem> infoItemCollection = new ObservableCollection<InfoItem>();
    public ObservableCollection<InfoItem> InfoItemCollection
    {
        get { return infoItemCollection; }
    }
    [NonSerialized]
    private ObservableCollection<InfoItem> infoItemSerachResult = new ObservableCollection<InfoItem>();
    public ObservableCollection<InfoItem> InfoItemSerachResult
    {
        get { return infoItemSerachResult; }
    }
    [NonSerialized]
    private string _SerachString;
    public string SerachString
    {
        set
        {
            _SerachString = value;
        }
        get
        {
            return _SerachString;
        }
    }
}


Inside the user control I have a textbox. When the user writes something inside the textbox, the text clears by changing between tabs. If I adjust some properties, then the text does not clear but is the same for all tabs. as if all tabs are using the same data, while I like textbox of each tab operates independent.

This is WPF source of my main window:
XML
<Window x:Class="InfoManager.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myUserControl="clr-namespace:InfoManager.Presentation_Layer"
        Title="MainWindow" Height="350" Width="507" Closed="Window_Closed">
        <Grid>
        <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>
        <Button Width="30" Height="30" HorizontalAlignment="Right"  Name="buttonAddTab" VerticalAlignment="Top" Margin="0,5,5,0" Click="buttonAddTab_Click" Grid.Column="1">
        <Image Source="..\resources\add.png" Stretch="UniformToFill"></Image>
        </Button>
    </Grid>
</Window>



and this is WPF source of my user control:
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?????????????????}"  Name="textBoxSelectionTags"  VerticalAlignment="Center" Margin="10" />
            </Grid>
            <ListView Grid.Row="1" Name="listView1" Margin="10" />
        </Grid>
    </Grid>
</UserControl>


I put the source code here, in case it is needed:
http://www.megaupload.com/?d=S0U05X63[^]

Thanks
Posted
Comments
Mark Salsbery 28-Jun-11 16:33pm    
*edit* Never mind. Downloaded your code...see possible solution below...

1 solution

The default UpdateSourceTrigger for the TextBlock is LostFocus, but apparently when the user clicks another tab the lostfocus event isn't raised so your source doesn't get updated. It does work if you click that upper-right button (which removes focus from the textblock) before switching tabs.

Try a different UpdateSourceTrigger on the binding, something like
Text="{Binding SerachString,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
 
Share this answer
 
v2
Comments
[no name] 28-Jun-11 17:24pm    
worked great. thanks

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


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