无法找到引用 RibbonComboBox 的绑定源 [英] Cannot find source for binding with reference with RibbonComboBox

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

问题描述

我正在尝试学习 WPF,但似乎遇到了一个我真的不完全理解的错误.

I'm trying to learn WPF and seem to be getting an error I really don't fully understand.

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

这是我的 XAML 的一部分.我不知道错误是否刚刚开始,但我一直在调整与 Home 标题下的两个 RibbonComboBoxe 一起工作的代码.( 元素位于 元素中.)

Here a section of my XAML. I can't tell if the errors just started, but I have been tweaking code that works with the two RibbonComboBoxees under the Home header. (The <Ribbon> element is in a <Grid> element.)

谁能帮我尝试解决这个问题?代码的行为不太正确,我无法判断此错误是否相关.

Can anyone help me to try and resolve this issue? The code isn't quite behaving correctly and I can't tell if this error is related.

<Ribbon>
    <Ribbon.ApplicationMenu>
        <RibbonApplicationMenu>
            <RibbonApplicationMenuItem Header="_Preview" ImageSource="Images/preview.png"></RibbonApplicationMenuItem>
            <RibbonSeparator></RibbonSeparator>
            <RibbonApplicationMenuItem Name="Save" KeyTip="S" Header="_Save" ImageSource="Images/save.png" ToolTipTitle="Save" ToolTipDescription="Save the current article and keep editing" Click="Save_Click"></RibbonApplicationMenuItem>
            <RibbonSeparator></RibbonSeparator>
            <RibbonApplicationMenuItem Name="SaveAndExit" KeyTip="X" Header="Save and E_xit" ToolTipTitle="Save and Exit" ToolTipDescription="Save the current article and close window" Click="SaveAndExit_Click"></RibbonApplicationMenuItem>
            <RibbonApplicationMenuItem Name="AbortAndExit" Header="_Abort Edit" ToolTipTitle="Abort Edit" ToolTipDescription="Close window without saving changes to this article" Click="AbortAndExit_Click"></RibbonApplicationMenuItem>
        </RibbonApplicationMenu>
    </Ribbon.ApplicationMenu>
    <RibbonTab Header="Home">
        <RibbonGroup Header="Category">
            <RibbonComboBox Label="Category:" SelectionBoxWidth="250">
                <RibbonGallery SelectedItem="{Binding Path=Categories.SelectedItem}">
                    <RibbonGalleryCategory IsSharedColumnSizeScope="True" DisplayMemberPath="Text" ItemsSource="{Binding Path=Categories.Items}">
                    </RibbonGalleryCategory>
                </RibbonGallery>
            </RibbonComboBox>
            <RibbonComboBox Label="Subcategory:" SelectionBoxWidth="250">
                <RibbonGallery SelectedItem="{Binding Path=Subcategories.SelectedItem}">
                    <RibbonGalleryCategory IsSharedColumnSizeScope="True" ColumnsStretchToFill="True" MaxColumnCount="1" DisplayMemberPath="Text" ItemsSource="{Binding Path=Subcategories.Items}">
                    </RibbonGalleryCategory>
                </RibbonGallery>
            </RibbonComboBox>
            <RibbonButton Label="Edit Categories" Command="local:EditCommands.Categories" SmallImageSource="Images\categories_sm.png" ToolTipTitle="Edit Categories" ToolTipDescription="Add, edit or delete categories and subcategories" ToolTipImageSource="Images\categories_sm.png"></RibbonButton>
        </RibbonGroup>
        <RibbonGroup Header="Author">
            <RibbonTwoLineText Text="jwood@softcircuits.com"></RibbonTwoLineText>
            <RibbonButton Label="Set Author" Command="local:EditCommands.Header1" SmallImageSource="Images\h1_sm.png" ToolTipTitle="Header 1" ToolTipDescription="Format selected text as a level 1 header" ToolTipImageSource="Images\h1.png"></RibbonButton>
        </RibbonGroup>
        <RibbonGroup Header="Settings">
            <RibbonCheckBox Name="chkApproved" Label="Approved"></RibbonCheckBox>
            <RibbonCheckBox Name="chkDeprecated" Label="Deprecated"></RibbonCheckBox>
            <RibbonButton Name="btnMetadata" Label="Metadata" SmallImageSource="Images\metadata_sm.png" ToolTipTitle="Metadata" ToolTipDescription="Edit article metadata" ToolTipImageSource="Images\metadata.png" Click="btnMetadata_Click"></RibbonButton>
        </RibbonGroup>
        <RibbonGroup Header="History">
            <RibbonButton Label="Edit Updates" Command="local:EditCommands.EditHistory" SmallImageSource="Images\history_sm.png" LargeImageSource="Images\history.png" ToolTipTitle="Edit Updates" ToolTipDescription="Edit update history for this article"></RibbonButton>
        </RibbonGroup>
    </RibbonTab>

更新:

根据 IlVic 的回答,我将 Window 更改为 RibbonWindow.但后来我收到以下错误.

Based on IlVic's answer, I changed my Window to a RibbonWindow. But then I'm getting the following error.

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

推荐答案

您的问题是由您的 Ribbon 控件引起的.如果您查看它的默认样式(您可以使用 ILSpy),您会发现这个 MultiDataTrigger:

Your issue is cause by your Ribbon control. If you take a look to its default style (you can use ILSpy), you will find this MultiDataTrigger:

<rd:MultiDataTrigger>
    <rd:MultiDataTrigger.Conditions>
        <rd:Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ribbon:RibbonWindow}, Path=IsActive}" Value="False" />
        <rd:Condition Binding="{Binding Path=(SystemParameters.IsGlassEnabled)}" Value="False" />
        <rd:Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsHostedInRibbonWindow}" Value="True" />
    </rd:MultiDataTrigger.Conditions>
    <rd:Setter TargetName="PART_TitleHost" Value="{windows:DynamicResource {x:Static SystemColors.InactiveCaptionTextBrushKey}}" Property="TextElement.Foreground" />
</rd:MultiDataTrigger>

如您所见,它的第一个条件是寻找类型为 RibbonWindow.您将很容易找到另一个 MultiDataTrigger,它作为查找 RibbonWindow 控件的 WindowState 属性的条件.

As you can see its first condition is looking for an ancestor whose type is RibbonWindow. You will easly find another MultiDataTrigger which as a condition that looks for WindowState property of a RibbonWindow control too.

我猜你的 XAML 被放在一个普通的窗口中.因此,您需要将 Window 对象替换为 RibbonWindow 对象,例如:

I guess that your XAML is put inside a normal Window. So you need to replace the Window object with a RibbonWindow one, something like:

<rib:RibbonWindow
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:rib="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        x:Class="[yourWindowClassName]">
    <!-- Put your Ribbon Control somewhere here -->
</rib:RibbonWindow>

希望能帮到你.

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

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