gpt4 book ai didi

wpf - 绑定(bind)到内联数组声明中的 DataContext 属性

转载 作者:行者123 更新时间:2023-12-04 06:06:57 30 4
gpt4 key购买 nike

我们有一些 XAML,它们可能看起来很奇怪,但显然是在第三方的功能区库控件中定义几个按钮所必需的。画廊有一个 ItemsControl.ItemsSource,XAML 用两个数组项填充它,这些数组项是一个自定义类型,它有一个位图属性和一个 ICommand 属性。一切看起来都很好,但我无法让数组项属性绑定(bind)到与窗口数据上下文有关的任何内容。我已经尝试了我知道的所有技巧,RelativeSource,ElementName 但无济于事。下面是 XAML:

<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%,我对这个 View 的任何其他绑定(bind)都没有问题,就在这个数组定义中。

我想我对我可以访问的对象的层次结构感到困惑,但在我看来,无论我是否在数组定义标记中绑定(bind),我仍然应该能够找到一个元素并绑定(bind)到它数据上下文。有时 XAML 似乎有不一致的陷阱,这会导致数小时的头疼,只是为了一些简单的事情。我意识到我可以在我的 View 模型中硬编码其中的一些,即在代码中创建我的数组项并绑定(bind)到它,但我想避免这样做,因为这意味着在代码中硬编码图像路径,我觉得图像的路径是标记声明。

任何帮助都感激不尽。

谢谢

保罗

最佳答案

您是否使用过“抛弃ElementName 并使用Sourcex: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}" ...

由于循环依赖而导致的外部化数组(您可以尝试保持原位,但我很确定编译器不会喜欢它)。

或者,您可以从管道对象中获取 DataContext:
<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}}">

关于wpf - 绑定(bind)到内联数组声明中的 DataContext 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8245116/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com