c# - WPF 功能区 : DataTemplate causes BindingExpression path error

标签 c# wpf datatemplate ribbon

我在使用 WPF RibbonControl(2010 年 10 月版)时遇到了一个小问题。我的想法是将 RibbonGroup 的 ItemsSource 属性绑定(bind)到我的 View 模型,并根据需要使用 DataTemplate 创建 RibbonButtons。这有效,但是当您显示窗口时它会导致绑定(bind)错误(每个按钮一个):

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“RibbonContentPresenter”(名称=“PART_ContentPresenter”)上找不到“IsDropDownOpen”属性。绑定(bind)表达式:Path=IsDropDownOpen; DataItem='RibbonContentPresenter' (Name='PART_ContentPresenter');目标元素是 'RibbonButton' (Name='');目标属性是“NoTarget”(类型“Object”)

这是一个代码片段,viewmodel被一个字符串数组代替,但是问题是一样的:

<ribbon:RibbonWindow x:Class="WpfRibbonApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" x:Name="RibbonWindow" Width="640" Height="480" >

    <ribbon:RibbonWindow.Resources>
        <x:Array x:Key="buttonArray" Type="sys:String">
            <sys:String>Button 1</sys:String>
            <sys:String>Button 2</sys:String>
            <sys:String>Button 3</sys:String>
            <sys:String>Button 4</sys:String>
            <sys:String>Button 5</sys:String>
            <sys:String>Button 6</sys:String>
        </x:Array>
        <DataTemplate x:Key="buttonTemplate">
            <ribbon:RibbonButton Label="{Binding}"  />
        </DataTemplate>        
    </ribbon:RibbonWindow.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ribbon:Ribbon>
            <ribbon:RibbonTab Header="Tab1">
                <ribbon:RibbonGroup Header="Group1"
                    ItemsSource="{StaticResource buttonArray}" 
                    ItemTemplate="{StaticResource buttonTemplate}"
                />
            </ribbon:RibbonTab>
        </ribbon:Ribbon>         
    </Grid>
</ribbon:RibbonWindow>

再次,它有效,但绑定(bind)错误有点烦人。有什么办法可以摆脱它?

最佳答案

绑定(bind)错误出现在 RibbonControlsLibrary 中 RibbonButton 的控件模板中。该样式定义了以下 MultiDataTrigger,并在 RibbonButton 用作另一个控件的一部分时使用,例如 RibbonSplitButton。

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen}" Value="True" />
        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="False" />
        <Condition Binding="{Binding Path=HighContrast, Source={x:Static shell:SystemParameters2.Current}}" Value="True" />
    </MultiDataTrigger.Conditions>
    <Setter TargetName="OuterBorder" Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" />
</MultiDataTrigger>

关于c# - WPF 功能区 : DataTemplate causes BindingExpression path error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4276010/

相关文章:

c# - 无法解析 System.Drawing.Bitmap 和 System.Drawing.ImageConverter

c# - 当控件来自 DataTemplate 时,BindingOperations.ClearBinding(...) 不起作用

c# - 如何通过 Windsor 的接口(interface)和命名空间注册类?

c# - 使 C++ 软件包和 C# 包访问相同枚举/常量整数的最佳方法

c# - 为什么我不能在函数级别声明一个委托(delegate)?

c# - 如何在 WPF 中获取 ArcSegment 的中点

c# - StringBuilder 是否只在循环场景中更受欢迎?

wpf - WPF中的复选框网格

c# - WPF : Set ListViewItem from textblock in the Listview within a DataTemplate

c# - 在 DataTemplate 上找不到 TargetType 属性