无法找到带有RibbonComboBox引用的绑定源

3

我正在学习WPF,但是遇到了一个我不太理解的错误。

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的一部分。我不确定错误是什么时候开始的,但我一直在调整与“主页”标题下的两个RibbonComboBoxes配合工作的代码。(<Ribbon>元素位于<Grid>元素中。)
有人能帮我尝试解决这个问题吗?代码的行为不太正确,我无法确定这个错误是否相关。
<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。但是接着我遇到了以下错误。

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')
1个回答

1
你的问题是由你的 Ribbon 控件引起的。如果你查看它的默认样式(可以使用 ILSpy),你会发现这个 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属性。

我猜您的XAML代码放在普通窗口中。因此,您需要将Window对象替换为RibbonWindow对象,类似于:

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

我希望它有所帮助。


谢谢,我不知道我需要那个。我仍然需要学习关于WPF和XAML的很多东西。我已经将您的答案标记为正确的答案,但如果您不介意再看一下,我现在在更改后遇到了一个类似但不同的错误。RibbonGalleryItem是我必须手动添加到我的XAML中的元素吗? - Jonathan Wood
很抱歉,@JonathanWood,我无法重现您的第二个问题。可能您应该添加更多细节(也许可以考虑创建一个新问题)关于您的XAML和代码。 - Il Vic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接