【问题标题】:itemscontrol in UWP doesnt bind to coordinates of observablecollection items [duplicate]UWP中的itemscontrol不绑定到observablecollection项目的坐标[重复]
【发布时间】:2017-02-25 10:55:28
【问题描述】:

我的代码没有绑定到可观察集合中项目的 X 和 Y 属性。怎么了:

    <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。 这是存储在 ObservableCollection LED 中的 Shape 类:

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

}

【问题讨论】:

    标签: c# uwp win-universal-app


    【解决方案1】:

    Setter.Value 属性的文档有以下注释:

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

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

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

    【讨论】:

    • 不知何故,它的绘图与 Canvas.Left 和 Canvas.Top 不完全相同。有什么想法吗?
    猜你喜欢
    • 2016-09-09
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多