TabControl中Tab的DataContextChanged过早引发

时间:2010-09-28 10:51:43

标签: wpf focus tabcontrol datacontext loaded

我有一些TabControl绑定到一些项目。在它下面是一个Button,我可以动态添加项目。在添加项目时,新项目应该成为活动Tab(使用TabControl.SelectedItem正常工作):

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:this="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TabControl ItemsSource="{Binding Items}"
                    SelectedItem="{Binding SelectedItem, Mode=OneWay}">
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <this:UserControl1 />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
        <Button Content="Foo" Click="Button_Click"/>
    </StackPanel>
</Window>

代码隐藏:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        public ObservableCollection<Foo> Items { get; set; }
        public Foo SelectedItem { get { return Items.Last(); } }
        public event PropertyChangedEventHandler PropertyChanged;

        public MainWindow()
        {
            Items = new ObservableCollection<Foo>();
            Items.Add(new Foo {Bar = "bar"});

            InitializeComponent();
            DataContext = this;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Items.Add(new Foo {Bar = "bar"});

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Items"));
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
            }
        }
    }

    public class Foo { public string Bar { get; set; } }
}

UserControl1如下所示:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBox/>
        <TextBox x:Name="_textBox"
             DataContextChanged="OnDataContextChanged"
             Text="{Binding Bar}" />
    </StackPanel>
</UserControl>

当用户点击标签时,它的代码隐藏应该集中_textBox并选择它的文本:

using System.Windows;

namespace WpfApplication1
{
    public partial class UserControl1
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void OnDataContextChanged(object sender,
                                          DependencyPropertyChangedEventArgs e)
        {
            _textBox.Focus();
            _textBox.SelectAll();
        }
    }
}

我尝试使用DataContextChanged事件实现这一点,但由于其不可预测性(s.f。http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.datacontextchanged.aspx),它不能一直工作。我也尝试使用Loaded-event,但是只有在加载DataTemplate时才会调用它。

因此,我认为每次DataContext发生更改并且数据绑定引擎完成其工作时,我都需要接收Loaded事件。有这样的事件吗?

1 个答案:

答案 0 :(得分:0)

您是否要在用户添加标签时选择文字?当用户点击其他标签时?

如果是这种情况,您可能希望使用两个事件处理程序来处理此问题 - 选项卡控件的选项卡更改事件 - 然后在添加新项目时在代码中设置它。

根据您的代码的DataContext不会更改。它被设置为主窗口,然后继承到子控件。

    public MainWindow()
    {
        Items = new ObservableCollection<Foo>();
        Items.Add(new Foo {Bar = "bar"});

        InitializeComponent();
        DataContext = this;
    }