本文介绍了找不到绑定源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加一个新选项卡然后将其删除时,我的应用程序会抛出此错误消息:

My application would throw this error message when I added a new tab and then deleted it:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.TabControl', AncestorLevel='1''. BindingExpression:Path=TabStripPlacement; DataItem=null; target element is 'TabItem' (Name=''); target property is 'NoTarget' (type 'Object')

如果我添加一个新标签,切换到另一个标签,切换回来,然后删除它,它并没有抱怨.似乎在切换过程中有些东西被更新"了,但我无法弄清楚是什么以及如何修复它们.

It didn't complain if I added a new tab, switched to another tab, switched back, and then deleted it. Seemed like something was "updated" during the switches, but I couldn't figure out what and how to fix them.

这是我的 xaml 文件:

This is my xaml file:

<Window x:Class="MyHomework__MVVM_.MyHomeworkView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="My Homework" Height="450" Width="800" ResizeMode="CanMinimize">
    <Grid Margin="0,0,10,10">
        <TabControl HorizontalAlignment="Left" Height="330" VerticalAlignment="Top" Width="764" Margin="10,10,0,0" ItemsSource="{Binding AllTabs}" SelectedItem="{Binding SelectedTab}">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Header" Value="{Binding Header}"/>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Grid>
                                    <TextBox Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="16" AcceptsReturn="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextChanged="OnTextChanged">
                                    </TextBox>
                                </Grid>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="FontSize" Value="20"/>
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>
        <Button Content="Add Course" HorizontalAlignment="Left" VerticalAlignment="Top" Width="105" Margin="10,351,0,0" Height="50" Command="{Binding AddCourseCommand}"/>
        <Button Content="Drop Course" HorizontalAlignment="Left" VerticalAlignment="Top" Width="76" Margin="126,379,0,0" Height="22" Command="{Binding DropCourseCommand, UpdateSourceTrigger=PropertyChanged}"/>
        <Button Content="Save HW" HorizontalAlignment="Left" VerticalAlignment="Top" Width="105" Margin="669,351,0,0" Height="50" Command="{Binding SaveHomeworkCommand, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

这是我添加/删除标签的代码:

And this is my codes for adding/deleting tabs:

public void AddNewTab()
        {
            NewCourseName ncn = new NewCourseName();
            ncn.Owner = mainWindow;
            ncn.ShowDialog();
            if (ncn.courseName != null)
            {
                MyHomeworkModel newTab = new MyHomeworkModel();
                newTab.Header = ncn.courseName;
                newTab.Text = "";
                AllTabs.Add(newTab);
                SelectedTab = newTab;
            }
        }

public void RemoveTab()
        {
            DropCourseConfirmation dcc = new DropCourseConfirmation();
            dcc.Owner = mainWindow;
            dcc.ShowDialog();
            if (dcc.drop == true)
            {
                int index = AllTabs.IndexOf(SelectedTab);
                AllTabs.Remove(SelectedTab);

                if (AllTabs.Count > 0)
                {
                    if (index == 0)
                    {
                        SelectedTab = AllTabs[0];
                    }
                    else
                    {
                        SelectedTab = AllTabs[--index];
                    }
                }
                else
                {
                    SelectedTab = null;
                }
            }
        }

如果您需要查看更多代码,请告诉我.提前致谢.

Let me know if you need to see more codes. Thanks in advance.

推荐答案

正如 Zarat 中提到的 TabItem 的默认样式Windows 8 具有在删除后触发的触发器,然后查找现在丢失的 TabControl.我认为这是一个错误,因为添加和删除 TabItems 是一个非常常见的场景,不是吗?

As Zarat mentioned the default style for TabItem in Windows 8 has triggers that fire after the remove and then look for the now missing TabControl. I consider that a bug because adding and removing TabItems is a really common scenario isn't it?

我发现作为一种解决方法,可以删除 TabItem 的模板:

I found as a workaround, that it is possible to remove the template of the TabItem:

foreach (var item in TabControl.Items)
{
    var tabitem = item as TabItem;
    // if this is the item to remove
    tabitem.Template = null;
    TabControl.Items.Remove(item);
}

这在我的场景中看起来没问题,因为我不会再使用 TabItem.

That looks ok in my scenario, because I will not use the TabItem any more.

我也尝试清除模板的触发器集合或清除其触发器的条件集合,但不允许这样做(错误).
似乎也没有办法禁用触发器.

I also tried clearing the triggers collection of the template or clearing the conditons collection of its triggers, but it is not allowed to do that (errors).
Also there does not seem to be a way tot disable the triggers.

这篇关于找不到绑定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 07:32