【问题标题】:Canvas binding in silverlight银光中的帆布装订
【发布时间】:2011-06-13 20:34:12
【问题描述】:

我正在尝试创建一个画布,项目位于画布上的指定位置,因为我无法将源和模板直接绑定到画布,我是否使用了 ItemsControl。 但是有一个问题,所有项目都位于 0,0。我已经测试了它们不返回 0,0 的绑定。 我怎样才能完成这项工作,以便将物品放置在正确的位置?

是否也可以在画布上创建 2 个图层,其中每个图层绑定到不同的源并使用不同的模板?

这是在 Silverlight 中

<ItemsControl Grid.Row="1" Grid.Column="1"
                Width="650" Height="650"
                ItemsSource="{Binding Skills}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Margin="0"
                Width="650" Height="650" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Canvas.Top="{Binding Top}" Canvas.Left="{Binding Left}">
                    <TextBlock Text="{Binding Name}" />
                <Image Source="{Binding Icon}" />
                <StackPanel Orientation="Horizontal" >
                    <TextBlock FontWeight="Bold" TextAlignment="Center" Text="{Binding SkillPointsStatusText}" />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

使用 ItemContainerStyle 进行测试

<ItemsControl Grid.Row="1" Grid.Column="1"
                Width="650" Height="650"
                ItemsSource="{Binding Skills}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Margin="0"
                Width="650" Height="650" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}" />
                <Image Source="{Binding Icon}" />
                <TextBlock FontWeight="Bold" TextAlignment="Center" Text="{Binding SkillPointsStatusText}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Canvas.Top" Value="{Binding Top}" />
            <Setter Property="Canvas.Left" Value="{Binding Left}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

好吧,我已经放弃了这个项目,但是如果有人有答案,我会留下这个问题

【问题讨论】:

  • 我加了一个赏金,因为当你用我的非工作答案问这个问题时,我几乎扼杀了这个问题,对此感到抱歉。希望这会给它应有的关注。
  • 你能贴出你想绑定的类的代码吗?

标签: wpf silverlight xaml data-binding


【解决方案1】:

以下所有内容都不适用于 SL4,因为它依赖于 Setter.Value 中的绑定。


尝试在ItemContainerStyle 中设置绑定,因为您的StackPanel 不是根元素;您的模板将放置在ContentPresenter 中,因此您在StackPanel 中定位画布的附加属性将被忽略。

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

编辑:如果 Silverlight 不支持 ItemContainerStyle,您可以为 ContentPresenters 设置通用样式,它应该也可以工作:

    <ItemsControl ItemsSource="{Binding Data}">
        <ItemsControl.Resources>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="{Binding Left}"/>
                <Setter Property="Canvas.Top" Value="{Binding Top}"/>
            </Style>
        </ItemsControl.Resources>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                ...
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

【讨论】:

  • 这个 要插入到哪里?我不断收到错误“在类型'ItemsControl'中找不到可附加属性'ItemContainerStyle'”。
  • 它不是附加属性,您可以将它作为 ItemsControl 的子项,就像使用 一样。即它确实属于 ItemTemplate。
  • 查看我是否已将其插入到第一篇文章中,这给了我错误,错误 1 ​​XML 命名空间 'schemas.microsoft.com/winfx/2006/xaml/presentation' 中的类型 'ItemsControl' 上不存在属性 'ItemContainerStyle' .
  • 哦,我已经在 WPF 中对其进行了测试并且它可以工作,难道这个属性在 Silverlight 中根本不存在?我添加了另一种不使用该属性的方法。
  • 嗯,现在它给了我一个错误“无法设置只读属性''。”当我插入 时。它似乎不是资源本身,而是解决问题的 2 个
猜你喜欢
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
相关资源
最近更新 更多