UWP中的itemscontrol不会绑定到可观察集合项的坐标 [英] itemscontrol in UWP doesnt bind to coordinates of observablecollection items

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

问题描述

我的代码未绑定到可观察集合中项目的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>

它确实绑定到Color和FillColor。
这是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;
    }

}


推荐答案

设置器的文档。 Value 属性具有以下注释:


Windows Presentation Foundation(WPF)和Microsoft Silverlight支持使用绑定表达式的功能为样式中的Setter提供值。 Windows运行时不支持Setter.Value的绑定用法(绑定不会评估,并且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不会绑定到可观察集合项的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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