无法为绑定找到源 [英] Cannot find source for binding

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

问题描述

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

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>

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

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

让我知道如果你需要查看更多codeS。先谢谢了。

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.

我也试过清除模板触发器采集或清除其触发的conditons集合,但不允许这样做(错误)。结果
也似乎没有被禁用TOT触发器的方式。

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.

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

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