绑定到DataContext属性的内联阵里面宣言 [英] Bind to DataContext Property inside inline Array Declaration

查看:103
本文介绍了绑定到DataContext属性的内联阵里面宣言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些XAML这似乎很奇怪,但显然需要定义一个第三方的带状画廊控件内几下即可。画廊有其XAML与两个数组项填充ItemsControl.ItemsSource,这些阵列项目是其中有一个位图属性和属性的ICommand一个自定义类型。一切看起来都很好,但我不能让数组项属性绑定到任何与窗口的数据上下文。我已经尽了浑身解数,我知道,的RelativeSource,但的ElementName不得要领。下面是XAML:

We have some XAML which may seem odd but apparently is needed for defining a couple of buttons inside a third party's ribbon gallery control. The gallery has a ItemsControl.ItemsSource which the XAML is populating with two Array items, these array items are a custom type which has a bitmap property and a ICommand property. Everything looks fine but I cannot get the array item property to bind to anything to do with the window's data context. I have tried every trick i know, RelativeSource, ElementName but to no avail. Below is the 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%,我想要的,我有这种观点的任何其他绑定没有问题,只是在这个数组定义

Note : MainView is the name of the window, the data context is 100% what I want, I have no problems with any other bindings on this view, just within this array definition.

我想我得到一个懵懵懂懂的问候我有但在我看来访问对象的层次,无论我是否数组定义标记内部结合我应该还是能够找到一个元素,绑定到它的数据上下文。有时XAML似乎有刚刚简单的东西创造挠头小时,inconsitency陷阱。我知道我可以硬code在我的视图模型,即在code创建我的阵列项目并绑定到一些,但我想避免这样做,因为这将意味着有图像路径硬codeD在code,我觉得路径图像是一个标记声明。

I guess I'm getting in a muddle with regards to the hierarchy of the objects i have access to but in my opinion regardless of whether I'm binding inside the array definition markup i should still be able to find an element and bind to it's data context. Sometimes XAML seems to have inconsitency gotchas which creates hours of head scratching, just for something simple. I realise I could hard code some of this in my View Model, i.e. create my array items in the code and bind to that but I want to avoid doing that as it would mean having image paths hardcoded in code, i feel paths to images are a markup declaration.

任何帮助将非常AP preciated。

Any help will be much appreciated.

感谢

推荐答案

让您使用的的ElementName ,并使用源代码< ?/ code>&放大器; X:参考的欺骗又

Have you used the "ditch ElementName and use Source & x:Reference" trick yet?

<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}" ...

外化阵列,由于周期性的依赖(你可以尝试守在原地,但我pretty确保编译器不会喜欢它)。

Externalized array due to cyclical dependency (you can try to keep in place but i'm pretty sure the compiler won't like it).

另外,你可以从一个管道对象抢夺的DataContext:

Alternatively you can snatch the DataContext from a pipe object:

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

这篇关于绑定到DataContext属性的内联阵里面宣言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆