您的位置:首页 > 其它

如何在WPF的ListBox中根据数据自定义Item的位置?附答案

2009-02-19 17:21 1101 查看
话说,包包想在游戏大厅里实现只操作数据源就能更新绑定控件的需求。OK,你一定会说,用ListBox。没错,ListBox就是干这个用的:

<UserControl x:Class="TestDeskBindingWpfApplication.PaneDesk"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestDeskBindingWpfApplication"
Height="160" Width="160">
<UserControl.Resources>
<local:ImageConverter x:Key="imageConverter" />
<local:HandConverter x:Key="handConverter" />
<local:LeftConverter x:Key="leftConverter" />
<local:TopConverter x:Key="topConverter" />

<Style x:Key="positionStyle">
<Setter Property="Canvas.Left" Value="{Binding Path=DeskPosition, Converter={StaticResource leftConverter}}" />
<Setter Property="Canvas.Top" Value="{Binding Path=DeskPosition, Converter={StaticResource topConverter}}" />
</Style>

<DataTemplate DataType="{x:Type local:UserInfo}">
<Canvas>
<Image Name="image"
Source="{Binding Path=ImageIndex, Converter={StaticResource imageConverter},Mode=TwoWay, NotifyOnTargetUpdated=True}" MouseDown="image_MouseDown" />
<Image Name="hand"
Source="Img/hand.gif"
Visibility="{Binding Path=IsPrepareOK, Converter={StaticResource handConverter},Mode=TwoWay, NotifyOnTargetUpdated=True}" />
<Label Name="lblUser"
Content="{Binding Path=UserName, Mode=TwoWay, NotifyOnTargetUpdated=True}"
Width="54" />
</Canvas>
</DataTemplate>
</UserControl.Resources>

<Canvas Background="#FF28699E">
<Image Canvas.Left="107" Canvas.Top="50" Name="chair1" Source="Img/Chair/chair1.gif" MouseDown="image_MouseDown" />
<Image Canvas.Left="60" Canvas.Top="102" Name="chair2" Source="Img/Chair/chair2.gif" />
<Image Canvas.Left="15" Canvas.Top="50" Name="chair3" Source="Img/Chair/chair3.gif" />
<Image Canvas.Left="60" Canvas.Top="3" Name="chair4" Source="Img/Chair/chair4.gif" />

<Label Canvas.Left="54" Canvas.Top="143" Name="lblDeskNO" Content="cc" />

<Image Canvas.Left="55" Canvas.Top="45" Name="imgDesk" Source="Img/picStatus.png" Width="50" Height="56" />

<ItemsControl x:Name="users" ItemsSource="{Binding Users}" ItemContainerStyle="{StaticResource positionStyle}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Width="160" Height="160" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Canvas>
</UserControl>

感谢Sean的帮助,这个答案是最合我意的。

感谢 YoHanwinter-cn的热心参与。winter-cn的答案与上述大同小异,但老兄你是否发现,点击第一个Item会有一个底色背景(因为仍然继承ListBox而延续了SelectItem的原因);YoHan的Demo做的有点复杂了,hoho,但还是感谢你花那么多时间写那么多代码做出相同的效果来。

就不感谢周银辉、赖仪灵和颗粒宁博爱了哦!嘿嘿。

本贴正式结贴!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐