如何在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的帮助,这个答案是最合我意的。
感谢 YoHan和winter-cn的热心参与。winter-cn的答案与上述大同小异,但老兄你是否发现,点击第一个Item会有一个底色背景(因为仍然继承ListBox而延续了SelectItem的原因);YoHan的Demo做的有点复杂了,hoho,但还是感谢你花那么多时间写那么多代码做出相同的效果来。
就不感谢周银辉、赖仪灵和颗粒宁博爱了哦!嘿嘿。
本贴正式结贴!
<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的帮助,这个答案是最合我意的。
感谢 YoHan和winter-cn的热心参与。winter-cn的答案与上述大同小异,但老兄你是否发现,点击第一个Item会有一个底色背景(因为仍然继承ListBox而延续了SelectItem的原因);YoHan的Demo做的有点复杂了,hoho,但还是感谢你花那么多时间写那么多代码做出相同的效果来。
就不感谢周银辉、赖仪灵和颗粒宁博爱了哦!嘿嘿。
本贴正式结贴!
相关文章推荐
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- WPF开发中遇到的问题及解决系列(二):如何获取通过binding生成的ComboBox或者ListBox等的Item
- WPF关于改变ListBoxItem的颜色的注意事项以及如何找到ListBox中的ItemsPanel
- WPF 自定义listbox的ItemTemplate实现通讯录
- WPF 自定义的图表(适用大量数据绘制)
- List添加Item时, 如何去判断某个字段, 并且保证字段数据不重复性
- Win10系统如何根据需要自定义主题颜色
- wpf,ListBox,ScrollViewer内容向左向右偏移指定位置
- WPF如何获取控件之间的相对位置
- MFC如何自定义消息与在线程中向主窗体控件发送数据
- Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象
- 如何在Qt中使用自定义数据类型
- Golang 如何定义一个接口类型的切片,它可以用来存储混合类型的数据,又如何自定义错误信息输出,以及如何定义变参函数,还有字符串多种拼接方式
- List添加Item时, 如何去判断某个字段, 并且保证字段数据不重复性
- 关于如何使自定义的Button和系统的UIBarButtonItem保持一致的两种方法
- 如何在列表、字典、集合中根据条件筛选数据
- abap--如何根据动态公式计算数据(增补),谢谢网友补充
- Hadoop(十):简单了解Hadoop数据类型,输入输出格式及用户如何自定义。
- 如何在 Repeater 的事件中得到 当前 Item 绑定的数据 ---声明转载
- 如何自定义数据类型