UWP 中的 itemscontrol 不绑定到 observablecollection 项的坐标 [英] itemscontrol in UWP doesnt bind to coordinates of observablecollection items

查看:25
本文介绍了UWP 中的 itemscontrol 不绑定到 observablecollection 项的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码不会绑定到可观察集合中项目的 X 和 Y 属性.出了什么问题:

My Code does not bind to the X and Y property of the items in the observable Collection. What is wrong:

    <ItemsControl ItemsSource="{Binding LED}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas Background="SkyBlue"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="{Binding X}" />
                <Setter Property="Canvas.Top" Value="{Binding Y}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Ellipse Stroke="{Binding Color}" Fill="{Binding FillColor}" StrokeThickness="1" Width="40" Height="40"></Ellipse>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

它确实绑定到颜色和填充颜色.这是 Shape 类,它存储在 ObservableCollection LED 中:

It does bind to Color and FillColor. Here is the Shape class, that is stored in the ObservableCollection LED:

class Shape
{
    public int X { get; private set; }
    public int Y { get; private set; }
    public string Color { get; private set; }
    public string FillColor { get; private set; }

    public Shape (int x, int y, string color, string fillColor)
    {
        X = x;
        Y = y;
        Color = color;
        FillColor = fillColor;
    }

}

推荐答案

Setter.Value 属性有如下注释:

Windows Presentation Foundation (WPF) 和 Microsoft Silverlight 支持使用绑定表达式为样式中的 Setter 提供值的能力.Windows 运行时不支持 Setter.Value 的 Binding 用法(不会评估 Binding 并且 Setter 无效,您不会收到错误,但也不会获得所需的结果). 从 WPF 或 Silverlight XAML 转换 XAML 样式时,将任何绑定表达式用法替换为设置值的字符串或对象,或者将值重构为共享的 {StaticResource} 标记扩展值,而不是绑定获得的值.

Windows Presentation Foundation (WPF) and Microsoft Silverlight supported the ability to use a Binding expression to supply the Value for a Setter in a Style. The Windows Runtime doesn't support a Binding usage for Setter.Value (the Binding won't evaluate and the Setter has no effect, you won't get errors, but you won't get the desired result either). When you convert XAML styles from WPF or Silverlight XAML, replace any Binding expression usages with strings or objects that set values, or refactor the values as shared {StaticResource} markup extension values rather than Binding-obtained values.

作为一种解决方法,您可以尝试使用 RenderTransform:

As a workaround, you could try using a RenderTransform instead:

<Ellipse Stroke="{Binding Color}" Fill="{Binding FillColor}" StrokeThickness="1" Width="40" Height="40">
    <Ellipse.RenderTransform>
        <TranslateTransform X="{Binding X}" Y="{Binding Y}"/>
    </Ellipse.RenderTransform>
</Ellipse>

这篇关于UWP 中的 itemscontrol 不绑定到 observablecollection 项的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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