我该怎么做在ItemContainerStyle绑定在WinRT中? [英] How do I do bindings in ItemContainerStyle in WinRT?

查看:188
本文介绍了我该怎么做在ItemContainerStyle绑定在WinRT中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想收集绑定到一个ItemsControl,用帆布作为项目小组,并与每个项目的Canvas.Left和Top绑定到项目对象的属性。基本上我试图重新建立我的这个职位上我的博客,但这次在WinRT中,而不是WPF。

I'm trying to bind a collection to an ItemsControl, with a Canvas as the items panel, and with each item's Canvas.Left and Top bound to properties on the item objects. Basically I'm trying to re-create the 2-D databinding I described in this post on my blog, but this time in WinRT instead of WPF.

由于ItemsControl的包装在另一个UI元素您的ItemTemplate内容(内容presenter,在WinRT中的情况下),并且它是直接放置的物品面板内那些包装/容器元素,Left和Top有关于这些容器进行设置;你不能只将它们设置在DataTemplate中。在WPF中,它很容易与在ItemContainerStyle,例如绑定要做到这一点:

Since ItemsControl wraps your ItemTemplate content in another UI element (a ContentPresenter, in the case of WinRT), and it's those wrapper/container elements that are placed directly inside the items panel, the Left and Top have to be set on those containers; you can't just set them in the DataTemplate. In WPF, it's easy enough to do this with bindings in the ItemContainerStyle, e.g.:

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Canvas.Left" Value="{Binding Path=X}"/>
        <Setter Property="Canvas.Top" Value="{Binding Path=Y}"/>
    </Style>
</ItemsControl.ItemContainerStyle>

但是,当我尝试在WinRT中/ XAML项目同样的事情,我什么也没得到。 甚至没有绑定错误的如果我硬code的值,它的工作原理;但如果我使用绑定,物业只是停留在它的默认值(零),并没有约束力的错误在输出窗口中显示。

But when I try the same thing in a WinRT/XAML project, I get nothing. Not even binding errors. If I hard-code a value, it works; but if I use a binding, the property just stays at its default value (zero), and no binding errors are shown in the Output window.

<ItemsControl.ItemContainerStyle>
    <Style TargetType="ContentPresenter">
        <!-- This works, so ItemContainerStyle does work in WinRT: -->
        <Setter Property="Canvas.Left" Value="200"/>
        <!-- But this silently fails, leaves Top as 0, and does not show
             any binding errors in the debugger's Output window: -->
        <Setter Property="Canvas.Top" Value="{Binding Y}"/>
    </Style>
</ItemsControl.ItemContainerStyle>

我验证过的内容presenters做了正确的DataContext(即集合项目,而不是集合本身还是其他什么东西时髦的),所以你会觉得这些绑定会工作得很好。但他们甚至不似乎得到的评估的如果我把一个坏结合其他地方,并运行调试版本,我看到在调试器的输出窗口绑定错误。但如果我引用我ItemContainerStyle内废话财产,不绑定错误显示。

I've verified that the ContentPresenters do have the correct DataContext (i.e. the collection item, not the collection itself or something else funky), so you'd think these bindings would work just fine. But they don't even seem to get evaluated. If I put a bad binding anywhere else, and run a debug build, I see binding errors in the debugger's Output window; but if I reference a nonsense property inside my ItemContainerStyle, no binding errors are shown.

下面是一个更完整的例子,即(据我所知)应该在WPF做工精细,但在WinRT中的起源叶的一切:

Here's a more complete example, that (as far as I know) should work fine in WPF, but that leaves everything at the origin in WinRT:

<ItemsControl ItemsSource="{Binding Tiles}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Left" Value="{Binding DataContext.Left}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle Width="80" Height="80" Fill="Gray"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我试过几个比较奇异期权在绑定 - 特别是的RelativeSource 。当我用的RelativeSource TemplatedParent 中,什么也不做的行为不变。然而,当我使用的RelativeSource自,我没有得到一个错误的结合,他说,物业没有对类型存在二传手!它采取了有点望文生义,在那里。

I've tried a few of the more exotic options on Binding -- specifically RelativeSource. When I used RelativeSource TemplatedParent, the do-nothing behavior was unchanged. However, when I used RelativeSource Self, I did get a binding error, saying that the property didn't exist on type Setter! It's taking that Self a little too literally, there.

我也与 TemplateBinding 发挥各地,但我从来没有真正grokked什么是应该用于和所有我得到的是一些INCOM prehensible COM错误(欢迎WinRT的,落后的一个巨大的技术步骤)。

I also played around with TemplateBinding, but I never really grokked what that's supposed to be used for, and all I got was some incomprehensible COM errors (welcome to WinRT, a huge technological step backward).

我怎么能是(a)使绑定正常工作(都在还有其他的选择绑定,我可以用它来强制其正常工作?),或( b)以允许项目我的 ItemsContainer 要在任意定位在画布基于数据绑定到性能的藏品?

How can I either (a) make the bindings work correctly (are there other options on Binding that I could use to coerce it to work properly?), or (b) otherwise allow items in my ItemsContainer to be positioned arbitrarily on a Canvas based on databindings to properties on the collection items?

推荐答案

绑定不支持setter方法​​。我想,如果在所有的Silverlight只拿到了他们在5个版本。对于解决办法,你可以看看我的文章较旧的<一个href=\"http://blog.onedevjob.com/2011/10/26/workaround-for-binding-not-supported-on-stylesettervalue-in-silverlight/\">here.基本上你定义设置绑定你的一个附加依赖项属性。

Bindings are not supported on Setters. I think Silverlight only got them in version 5 if at all. For workarounds you can look at my older article here. Basically you define an attached dependency property that sets up the binding for you.

这篇关于我该怎么做在ItemContainerStyle绑定在WinRT中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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