Canvas.Left 和 Canvas.Top 的 Setter 在 WPF 中是可读写的,但在 Silverlight 4 中却不是,为什么?
我有以下 XAML,它在 WPF 中工作正常,但在 Silverlight 4 中不行
<ItemsPanelTemplate x:Key="ContentListBoxItemsPanelTemplate">
<Canvas/>
</ItemsPanelTemplate>
<DataTemplate x:Key="ContentListBoxItemTemplate">
<Border CornerRadius="15" Width="150" Margin="3" Height="300">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="OrangeRed" Offset="1" />
<GradientStop Color="Brown" Offset="0" />
</LinearGradientBrush>
</Border.Background>
</Border>
</DataTemplate>
<Style TargetType="ListBoxItem">
<Setter Property="Canvas.Left" Value="{Binding Left}"/>
<Setter Property="Canvas.Top" Value="{Binding Top}"/>
</Style>
然后在某个地方:
<ListBox Name="ContentList"
ItemTemplate="{StaticResource ContentListBoxItemTemplate}"
ItemsPanel="{StaticResource ContentListBoxItemsPanelTemplate}" />
如果我在 Silverlight 中尝试相同的操作,我会得到一个异常,指出 setter 无法设置只读属性,但我仍然想实现在 Silverlight 中也有同样的事情,无需代码。
有什么建议吗?
I have the following XAML, which works fine in WPF, but not in Silverlight 4
<ItemsPanelTemplate x:Key="ContentListBoxItemsPanelTemplate">
<Canvas/>
</ItemsPanelTemplate>
<DataTemplate x:Key="ContentListBoxItemTemplate">
<Border CornerRadius="15" Width="150" Margin="3" Height="300">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="OrangeRed" Offset="1" />
<GradientStop Color="Brown" Offset="0" />
</LinearGradientBrush>
</Border.Background>
</Border>
</DataTemplate>
<Style TargetType="ListBoxItem">
<Setter Property="Canvas.Left" Value="{Binding Left}"/>
<Setter Property="Canvas.Top" Value="{Binding Top}"/>
</Style>
And then somewhere:
<ListBox Name="ContentList"
ItemTemplate="{StaticResource ContentListBoxItemTemplate}"
ItemsPanel="{StaticResource ContentListBoxItemsPanelTemplate}" />
If I try the same thing in Silverlight I get an exception saying that the setter cannot set a read only property, but I still want to achieve the same thing in Silverlight without code.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Silverlight 不支持 setter 值的绑定。 David Anson 这里有一个很好的解决方法:http://blogs.msdn.com/delay/archive/2009/05/07/one-more-platform-difference-more-or- less-tamed-settervaluebindinghelper-makes-silverlight-setters-better.aspx
Silverlight does not support bindings in the value of the setter. David Anson has a great workaround here: http://blogs.msdn.com/delay/archive/2009/05/07/one-more-platform-difference-more-or-less-tamed-settervaluebindinghelper-makes-silverlight-setters-better.aspx