WPF 功能区:DataTemplate 导致 BindingExpression 路径错误
我在使用 WPF RibbonControl(2010 年 10 月版本)时遇到了一个小问题。我的想法是将 RibbonGroup 的 ItemsSource 属性绑定到我的视图模型,并使用 DataTemplate 根据需要创建 RibbonButton。这有效,但当您显示窗口时,它会导致绑定错误(每个按钮一个):
System.Windows.Data Error: 40 : BindingExpression path error: 'IsDropDownOpen' property not find on 'object' ''RibbonContentPresenter '(名称='PART_ContentPresenter')'。 BindingExpression:Path=IsDropDownOpen; DataItem='RibbonContentPresenter'(名称='PART_ContentPresenter');目标元素是“RibbonButton”(名称=“”); target property is 'NoTarget' (type 'Object')
这是一个代码片段,视图模型被字符串数组替换,但问题是相同的:
<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>
同样,它可以工作,但绑定错误是有点烦人。有什么办法可以摆脱它吗?
I've run into a small problem using the WPF RibbonControl (October 2010 version). My idea was to bind the ItemsSource property of a RibbonGroup to my viewmodel, and use a DataTemplate to create RibbonButtons as needed. This works, but it causes a binding error (one for each button) when you show the window:
System.Windows.Data Error: 40 : BindingExpression path error: 'IsDropDownOpen' property not found on 'object' ''RibbonContentPresenter' (Name='PART_ContentPresenter')'. BindingExpression:Path=IsDropDownOpen; DataItem='RibbonContentPresenter' (Name='PART_ContentPresenter'); target element is 'RibbonButton' (Name=''); target property is 'NoTarget' (type 'Object')
Here is a code fragment, the viewmodel is replaced by an array of strings, but the issue is the same:
<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>
Again, it works, but the binding error is a bit annoying. Is there any way to get rid of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绑定错误位于 RibbonControlsLibrary 内 RibbonButton 的控件模板中。该样式定义了以下 MultiDataTrigger,并在 RibbonButton 用作另一个控件(如 RibbonSplitButton)的一部分时使用。
The binding error is in the control template for the RibbonButton within the RibbonControlsLibrary. The style has the following MultiDataTrigger defined and is used when the RibbonButton is used as part of another control, like the RibbonSplitButton.
我使用 ControlTemplate 设置了 RibbonButton 的样式,但没有使用 MultiDataTrigger,如下所示:
这不会导致绑定错误。
I have Styled the RibbonButton with a ControlTemplate and without the MultiDataTrigger as below:
This will not caused the binding errors.