wpf 找不到绑定源

rbl8hiat  于 2023-06-24  发布在  其他
关注(0)|答案(4)|浏览(198)

当我添加一个新标签然后删除它时,我的应用程序会抛出以下错误消息:

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

如果我添加一个新标签,切换到另一个标签,切换回来,然后删除它,它不会抱怨。看起来像是在切换过程中“更新”了一些东西,但我不知道是什么以及如何修复它们。
这是我的xaml文件:

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

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

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

如果你想看更多代码,请告诉我。先谢谢你了。

pdsfdshx

pdsfdshx1#

正如Zarat所提到的,Windows 8中TabItem的默认样式有触发器,这些触发器会在移除之后触发,然后寻找现在丢失的TabControl。我认为这是一个bug,因为添加和删除TabItems是一个非常常见的场景,不是吗?
我发现作为一种解决方法,可以删除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。
我还尝试清除模板的触发器集合或清除其触发器的conditons集合,但不允许这样做(错误)。
而且似乎没有办法禁用触发器。

wnavrhmk

wnavrhmk2#

这不是一个bug,只是WPF绑定引擎在更新绑定时发出一些噪音,并注意到丢失了一些东西。不幸的是,它不能沉默。也许值得在Connect或MSDN论坛上报告,但不要期望任何快速React。
您注意到的消息来自aero2.normalcolor.xaml -Windows 8的默认样式。如果你在默认位置安装了VS 2012 SP 2,你可以在这里找到它们:C:\Program Files(x86)\Microsoft Visual Studio 11.0\Blend\SystemThemes\Wpf
在这个文件中有几个MultiDataTrigger,它们的条件是检查RelativeSource上的TabStripPlacement,查找父TabControl。因此,当您从TabControl中删除TabItem时,绑定引擎可能会尝试更新绑定,但发现父项丢失,并记录一个警告。这是完全可以的,因为TabItem被删除了,你不再关心样式(如果你要再次添加它,绑定将被重新评估,一切都会很好)。
现在我不知道为什么他们在Windows 8的RelativeSource上检索TabStripPlacement,因为TabItem本身似乎携带了其父TabStripPlacement的副本。所有其他默认样式都使用TabStripPlacement的本地副本进行绑定。因此,如果您觉得冒险,您可能希望将样式复制到自己的资源字典中,并在调试期间使用“固定”版本以减少噪音……

vdzxcuhz

vdzxcuhz3#

我相信这是最终的修复,它将在XAML级别工作(没有代码),应该对每个人都有效。

在您的TabControl中添加此 ItemContainerStyle

<TabControl ItemContainerStyle="{StaticResource FixTabStripTabItemStyle}"/>

并将此 Style 和此 ControlTemplate 添加到您的窗口/应用资源

<!--This should be used whenever a TabControl is using ItemsSource, as some Bindings are failing when the tabs are removed/added-->
<Style x:Key="FixTabStripTabItemStyle" TargetType="{x:Type TabItem}">
    <Setter Property="MinWidth" Value="60" />
    <Setter Property="Template" Value="{DynamicResource FixTabStripTabItemControlTemplate}"/>
</Style>

<ControlTemplate x:Key="FixTabStripTabItemControlTemplate" TargetType="{x:Type TabItem}">
    <Grid x:Name="templateRoot" SnapsToDevicePixels="True">
        <Border x:Name="mainBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" Background="{TemplateBinding Background}" Margin="0">
            <Border x:Name="innerBorder" BorderBrush="#FFACACAC" BorderThickness="1,1,1,0" Background="White" Margin="-1" Opacity="0"/>
        </Border>
        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Focusable="False" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
    </Grid>
    <ControlTemplate.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Left"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" TargetName="mainBorder">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFECF4FC" Offset="0"/>
                        <GradientStop Color="#FFDCECFC" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush" TargetName="mainBorder" Value="#FF7EB4EA"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,1,0,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,1,0,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Bottom"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" TargetName="mainBorder">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFECF4FC" Offset="0"/>
                        <GradientStop Color="#FFDCECFC" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush" TargetName="mainBorder" Value="#FF7EB4EA"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,0,1,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,0,1,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Right"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" TargetName="mainBorder">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFECF4FC" Offset="0"/>
                        <GradientStop Color="#FFDCECFC" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush" TargetName="mainBorder" Value="#FF7EB4EA"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="0,1,1,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="0,1,1,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" TargetName="mainBorder">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFECF4FC" Offset="0"/>
                        <GradientStop Color="#FFDCECFC" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush" TargetName="mainBorder" Value="#FF7EB4EA"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,1,1,0"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,1,1,0"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Left"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Opacity" TargetName="contentPresenter" Value="0.56"/>
            <Setter Property="Background" TargetName="mainBorder" Value="#FFF0F0F0"/>
            <Setter Property="BorderBrush" TargetName="mainBorder" Value="#FFD9D9D9"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,1,0,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,1,0,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Bottom"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Opacity" TargetName="contentPresenter" Value="0.56"/>
            <Setter Property="Background" TargetName="mainBorder" Value="#FFF0F0F0"/>
            <Setter Property="BorderBrush" TargetName="mainBorder" Value="#FFD9D9D9"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,0,1,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,0,1,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Right"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Opacity" TargetName="contentPresenter" Value="0.56"/>
            <Setter Property="Background" TargetName="mainBorder" Value="#FFF0F0F0"/>
            <Setter Property="BorderBrush" TargetName="mainBorder" Value="#FFD9D9D9"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="0,1,1,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="0,1,1,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Opacity" TargetName="contentPresenter" Value="0.56"/>
            <Setter Property="Background" TargetName="mainBorder" Value="#FFF0F0F0"/>
            <Setter Property="BorderBrush" TargetName="mainBorder" Value="#FFD9D9D9"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,1,1,0"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,1,1,0"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="false"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Left"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,1,0,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,1,0,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Left"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Panel.ZIndex" Value="1"/>
            <Setter Property="Margin" Value="-2,-2,0,-2"/>
            <Setter Property="Opacity" TargetName="innerBorder" Value="1"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,1,0,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,1,0,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="false"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Bottom"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,0,1,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,0,1,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Bottom"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Panel.ZIndex" Value="1"/>
            <Setter Property="Margin" Value="-2,0,-2,-2"/>
            <Setter Property="Opacity" TargetName="innerBorder" Value="1"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,0,1,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,0,1,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="false"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Right"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="0,1,1,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="0,1,1,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Right"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Panel.ZIndex" Value="1"/>
            <Setter Property="Margin" Value="0,-2,-2,-2"/>
            <Setter Property="Opacity" TargetName="innerBorder" Value="1"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="0,1,1,1"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="0,1,1,1"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="false"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,1,1,0"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,1,1,0"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Panel.ZIndex" Value="1"/>
            <Setter Property="Margin" Value="-2,-2,-2,0"/>
            <Setter Property="Opacity" TargetName="innerBorder" Value="1"/>
            <Setter Property="BorderThickness" TargetName="innerBorder" Value="1,1,1,0"/>
            <Setter Property="BorderThickness" TargetName="mainBorder" Value="1,1,1,0"/>
        </MultiDataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
nkoocmlb

nkoocmlb4#

我有一个类似的问题,我把我的样式放在app.xaml文件,并照顾它,我不再得到那些绑定错误。希望能帮上忙。

相关问题