在tabControl内的UserControl内绑定元素不起作用(WPF) [英] Binding an element inside a UserControl inside a tabControl does not work (WPF)

查看:130
本文介绍了在tabControl内的UserControl内绑定元素不起作用(WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 长话短说,我无法正确绑定文本框.

这是我程序的照片:

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:

<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:

[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;
        }
    }
}



在用户控件内部,我有一个文本框.当用户在文本框内写东西时,通过在选项卡之间切换来清除文本.如果我调整了某些属性,则文本不会清除,但所有选项卡都相同.好像所有选项卡都使用相同的数据一样,而我喜欢每个选项卡的文本框都是独立运行的.

这是我主窗口的WPF来源:



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:

<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>




这是我的用户控件的WPF来源:




and this is WPF source of my user control:

<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>



我将源代码放在这里,以备不时之需:
http://www.megaupload.com/?d=S0U05X63 [



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

Thanks

推荐答案

TextBlock的默认UpdateSourceTrigger是LostFocus,但是显然,当用户单击另一个选项卡时,不会引发lostfocus事件,因此您的源代码不会得到更新.如果在切换选项卡之前单击该右上角的按钮(该按钮可从文本块中删除焦点),它确实可以工作.

在绑定上尝试其他UpdateSourceTrigger,类似
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}"


这篇关于在tabControl内的UserControl内绑定元素不起作用(WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆