Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我的WPF TabControl至少包含一个选项卡时,如何确保它始终具有选定的选项卡?_Wpf_Silverlight_Xaml_Data Binding - Fatal编程技术网

当我的WPF TabControl至少包含一个选项卡时,如何确保它始终具有选定的选项卡?

当我的WPF TabControl至少包含一个选项卡时,如何确保它始终具有选定的选项卡?,wpf,silverlight,xaml,data-binding,Wpf,Silverlight,Xaml,Data Binding,我有一个选项卡控件,其项绑定到可观察集合: <TabControl ItemsSource="{Binding MyObservableCollection}" /> 随着项目从集合中添加和删除,选项卡将按预期添加和删除。但是,每当集合为空时,SelectedItem将恢复为-1(表示没有选定的选项卡)。然后,添加项目时,SelectedItem保持在-1,并且不选择新选项卡 如何使选项卡控件在向空集合添加项目时选择新选项卡?您最好覆盖“ontabaded”功能,以检查是否添加

我有一个
选项卡控件
,其项绑定到
可观察集合

<TabControl ItemsSource="{Binding MyObservableCollection}" />

随着项目从集合中添加和删除,选项卡将按预期添加和删除。但是,每当集合为空时,
SelectedItem
将恢复为-1(表示没有选定的选项卡)。然后,添加项目时,
SelectedItem
保持在-1,并且不选择新选项卡


如何使
选项卡控件
在向空集合添加项目时选择新选项卡?

您最好覆盖“ontabaded”功能,以检查是否添加了新选项卡(第一个),然后将SelectEditedIndex设置为0


由于您使用的是ObservaleCollection,您知道您的集合何时更改,因此我将从集合中订阅更改的事件并检查其中的项目数。

可能有一种更简单的方法,但是,您可以将collection changed事件挂接到VM中的ObservableCollection上,并将SelectedItem属性设置为新项(假设您已将所选项绑定到VM上的属性)。

如果您正在寻找纯MVVM实现,将索引属性添加到ViewModel,如果其中没有项目,则可以在CollectionChanged上设置Index=0。在XAML中,您可以如下所示绑定该索引

<TabControl ItemsSource="{Binding MyObservableCollection}" SelectedIndex="{Binding Index}" />

您可以订阅TabControl.ItemContainerGenerator.StatusChanged事件,如果状态为ContainerGenerated且TabControl的SelectedIndex为-1,则将TabControl的SelectedIndex设置为0

// peopleCollection is an ObservableCollection<Person>
People peopleCollection = new People();
public Window1()
{
    InitializeComponent();
    // MyTabControl is an instance of TabControl
    MyTabControl.ItemsSource = peopleCollection;
    MyTabControl.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
}

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
    if((sender as ItemContainerGenerator).Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated && MyTabControl.SelectedIndex == -1)
    {
        MyTabControl.SelectedIndex = 0; 
    }
}
//peopleCollection是一个可观察的集合
人物集合=新人物();
公共窗口1()
{
初始化组件();
//MyTabControl是TabControl的一个实例
MyTabControl.ItemsSource=peopleCollection;
MyTabControl.ItemContainerGenerator.StatusChanged+=新事件处理程序(ItemContainerGenerator\U StatusChanged);
}
void ItemContainerGenerator_状态已更改(对象发送者,事件参数e)
{
if((发件人为ItemContainerGenerator).Status==System.Windows.Controls.Primitives.GeneratorStatus.ContainerGenerated&&MyTabControl.SelectedIndex=-1)
{
MyTabControl.SelectedIndex=0;
}
}
有一些第三方解决方案具有这种开箱即用的功能。每当集合的状态从空变为“包含单个项”时,Telerik的RadTabControl就会选择第一个项

请在此处尝试演示:


注意:这是一个SL演示,但在WPF中工作原理相同。

我遇到了同样的问题,并通过将所选项目绑定到动态列表中的第一个项目来解决它

<TabControl ItemsSource="{Binding MyObservableCollection}" SelectedItem="{Binding MyObservableCollection.First}" />

为我工作:)



我已经尝试在CollectionChanged事件处理程序中将SelectedIndex设置为0,但没有效果-可能是在事件到达TabControl之前调用了我的处理程序。这正是我最初所做的,但我认为我是在TabControl获得事件之前处理该事件的,因此没有效果。我再试试——也许我做错了什么!确保您的索引属性设置程序引发propertyChanged事件我会使用SelectedItem而不是Index,但这更像是个人的事情。使用SelectedItem可以很好,但不使用SelectedIndex。不过,任务完成了@格雷姆-我说的是SelectedItem,不是index。。听起来你不同意我的观点:-)很高兴知道为什么SelectedItem有效,而SelectedIndex无效-有什么想法吗?
<TabControl ItemsSource="{Binding MyObservableCollection}" SelectedItem="{Binding MyObservableCollection[0]}" />