Wpf 绑定到内联数组声明内的DataContext属性
我们有一些XAML,它可能看起来很奇怪,但显然是在第三方的ribbon gallery控件中定义几个按钮所必需的。gallery有一个ItemsControl.ItemsSource,XAML用两个数组项填充它,这些数组项是一个自定义类型,具有位图属性和ICommand属性。一切看起来都很好,但我无法将array item属性绑定到与窗口的数据上下文有关的任何内容。我已经尝试了我知道的每一个技巧,相对资源,元素名称,但都没有用。以下是XAML:Wpf 绑定到内联数组声明内的DataContext属性,wpf,arrays,xaml,data-binding,itemscontrol,Wpf,Arrays,Xaml,Data Binding,Itemscontrol,我们有一些XAML,它可能看起来很奇怪,但显然是在第三方的ribbon gallery控件中定义几个按钮所必需的。gallery有一个ItemsControl.ItemsSource,XAML用两个数组项填充它,这些数组项是一个自定义类型,具有位图属性和ICommand属性。一切看起来都很好,但我无法将array item属性绑定到与窗口的数据上下文有关的任何内容。我已经尝试了我知道的每一个技巧,相对资源,元素名称,但都没有用。以下是XAML: <ribbon:RibbonGallery.
<ribbon:RibbonGallery.ItemsSource>
<x:Array Type="{x:Type customUITypes:ClickableImage}">
<customUITypes:ClickableImage x:Name="BitmapAddWorkflow" Command="{Binding ElementName=MainView, Path=DataContext.MyCommandOne}">
<customUITypes:ClickableImage.Bitmap>
<BitmapImage UriSource="/Images/GalleryWorkflowAdd.png"/>
</customUITypes:ClickableImage.Bitmap>
</customUITypes:ClickableImage>
<customUITypes:ClickableImage x:Name="BitmapDeleteWorkflow" Command="{Binding ElementName=MainView, Path=DataContext.MyCommandTwo}">
<customUITypes:ClickableImage.Bitmap>
<BitmapImage UriSource="/Images/GalleryWorkflowDelete.png"/>
</customUITypes:ClickableImage.Bitmap>
</customUITypes:ClickableImage>
</x:Array>
</ribbon:RibbonGallery.ItemsSource>
<ribbon:RibbonGallery.ItemTemplate>
<DataTemplate>
<Button Command="{Binding Command}">
<Image Margin="2" Source="{Binding Bitmap}" Stretch="None"/>
</Button>
</DataTemplate>
</ribbon:RibbonGallery.ItemTemplate>
注意:MainView是窗口的名称,数据上下文是我想要的100%,我对这个视图上的任何其他绑定都没有问题,就在这个数组定义中
我想我对我可以访问的对象的层次结构感到困惑,但在我看来,无论我是否在数组定义标记中绑定,我仍然应该能够找到一个元素并绑定到它的数据上下文。有时候,XAML似乎有一些不协调的问题,这会让人抓头好几个小时,只是为了一些简单的事情。我意识到我可以在我的视图模型中硬编码其中的一些,即在代码中创建数组项并绑定到它,但我想避免这样做,因为这意味着在代码中硬编码图像路径,我觉得图像路径是一种标记声明
任何帮助都将不胜感激
谢谢
Paul您使用过“ditchElementName
和使用Source
和x:Reference
技巧吗
<Window.Resources>
<x:Array x:Key="Items" Type="{x:Type customUITypes:ClickableImage}">
<customUITypes:ClickableImage x:Name="BitmapAddWorkflow"
Command="{Binding DataContext.MyCommandOne, Source={x:Reference MainWindow}}">
<customUITypes:ClickableImage.Bitmap>
<BitmapImage UriSource="/Images/GalleryWorkflowAdd.png"/>
</customUITypes:ClickableImage.Bitmap>
</customUITypes:ClickableImage>
<customUITypes:ClickableImage x:Name="BitmapDeleteWorkflow"
Command="{Binding DataContext.MyCommandTwo, Source={x:Reference MainWindow}}">
<customUITypes:ClickableImage.Bitmap>
<BitmapImage UriSource="/Images/GalleryWorkflowDelete.png"/>
</customUITypes:ClickableImage.Bitmap>
</customUITypes:ClickableImage>
</x:Array>
</Window.Resources>
<!-- ... -->
<ribbon:RibbonGallery ItemsSource="{StaticResource Items}" ...
非常有效,这就是我喜欢堆栈溢出的原因。非常感谢。出于好奇,在你回答的开头我是否感觉到一丝讽刺?我并不是想让我的问题听起来很自负,我很沮丧,因为我想不出来,你最后的答案很简单但很有效,我意识到我还有很多东西要用WPF学习。另外,感谢你在我的问题中格式化XAML,我想这最初是复制粘贴失败:)。Thanks@PaulieWaulie当前位置这根本不是讽刺,我只是意识到WPF中有很多“技巧”,我怀疑是否有人会完全了解它们。好吧,对不起,我是一个敏感的人:p,我只是有点偏执,我的问题措辞不好。无论如何,再次感谢您简洁而精彩的回答:0)。
<Window.Resources>
<!-- Resource declaration gives you easy access using StaticResource -->
<FrameworkElement Name="Pipe" Visibility="Hidden"/>
</Window.Resources>
<!-- Place it somewhere in the window where it can inherit the Window's DataContext -->
<StaticResource ResourceName="Pipe"/>
<!-- ... -->
<customUITypes:ClickableImage x:Name="BitmapAddWorkflow"
Command="{Binding DataContext.MyCommandOne, Source={StaticResource Pipe}}">