如何在 WinRT 的 ItemContainerStyle 中进行绑定? [英] How do I do bindings in ItemContainerStyle in WinRT?

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

问题描述

我正在尝试将集合绑定到 ItemsControl,将 Canvas 作为项目面板,并将每个项目的 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 将您的 ItemTemplate 内容包装在另一个 UI 元素(在 WinRT 的情况下为 ContentPresenter)中,并且这些包装器/容器元素直接放置在项目面板内,因此必须在这些元素上设置 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 项目中尝试同样的事情时,我一无所获.甚至没有绑定错误.如果我对一个值进行硬编码,它就可以工作;但是,如果我使用绑定,则该属性仅保持其默认值(零),并且输出"窗口中不会显示任何绑定错误.

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>

我已经验证 ContentPresenter 确实具有正确的 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>

我在 Binding 上尝试了一些更奇特的选项——特别是 RelativeSource.当我使用 RelativeSource TemplatedParent 时,什么都不做的行为没有改变.但是,当我使用 RelativeSource Self 时,我确实收到了一个绑定错误,说该属性在 Setter 类型上不存在!在那里,Self 有点过于字面化了.

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,但我从来没有真正理解它的用途,我得到的只是一些难以理解的 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) 使绑定正常工作(在 Binding 上是否还有其他选项可用于强制它正常工作?),或 (b) 以其他方式允许项目进入我的 ItemsContainer 是否基于集合项上属性的数据绑定被任意定位在 Canvas 上?

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 版中得到了它们.对于变通方法,您可以查看我的旧文章 此处.基本上,您定义了一个附加的依赖属性来为您设置绑定.

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.

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

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