XAML在资源字典中创建一个类的实例

weixin_38051012 2016-06-03 02:59:01
刚开始接触WPF和XAML,目前在写一个有UI的电梯调度算法程序作为练习

遇到了一个不知该如何解决的问题,我并没有在网上找到任何同类的问题(也许是我搜索的关键字不对?),所以只好发帖问了

我现在想在资源字典中定义一个静态资源,该资源是一个自定义的类的实例,包含一些泛型集合(错误提示似乎和泛型集合有关)

1)先说明一下我所定义的类

Buiding类,代表建筑,包含List<Floor>类型的Floors(建筑的所有楼层)和List<Elevator>类型的Elevators(建筑的所有电梯)成员对象,以及一些简单的string或int型属性

Floor类,代表楼层,目前仅需要关心它的Number(int)属性

Elevator类,代表电梯,包含Number(int)和List<int>ReachFloors属性,其中的List<int>ReachFloors代表该电梯可以达到哪些楼层

2)Dictionary.xaml文件(以下的"l"就是本程序集的命名空间)
<l:Building x:Key="MyBuilding" BuildingName="Building1"> <l:Building.Floors > <l:Floor Number="3" /> <l:Floor Number="2" /> <l:Floor Number="1" /> </l:Building.Floors> <l:Building.Elevators> <l:Elevator Number="0"> <l:Elevator.ReachFloors> <sys:Int32>3</sys:Int32> <sys:Int32>2</sys:Int32> <sys:Int32>1</sys:Int32> </l:Elevator.ReachFloors> </l:Elevator> <l:Elevator Number="1"> <l:Elevator.ReachFloors> <sys:Int32>2</sys:Int32> <sys:Int32>1</sys:Int32> </l:Elevator.ReachFloors> </l:Elevator> </l:Building.Elevators> </l:Building>


3)错误提示

上列2处中的错误提示分别为:

错误 1

值“newElevator.Floor”不是“newElevator.Floor”类型,不能在此泛型集合中使用。
参数名: value

错误 2

值“newElevator.Elevator”不是“newElevator.Elevator”类型,不能在此泛型集合中使用。
参数名: value

4)引发这个问题最初的起因

我想在主窗体中根据Building实例的楼层数和电梯数,生成对应数量的电梯内部面板按钮(不同的电梯面板按钮可能不同)、电梯矩形(用来制作电梯运动的动画)以及楼层按钮(包含UP按钮和DOWN按钮),并给每一个按钮Binding一个定义在Floor或Elevator中的命令。

最开始我是将Buiding实例的创建放在了MainWindow.xaml.cs中,用DataContext = new Building("建筑1", 6, 3);将该实例添加到MainWindow的DataContext中

ItemsControl和Button的关系大致如下(省略掉了关于Template和ItemsPanel的内容,我认为它们与问题无关)

<ItemsControl x:Name="Elevator" ItemsSource="{Binding Elevators}"> <ItemsControl.Template/> <ItemsControl.ItemsPanel/> <ItemsControl.ItemTemplate> <DataTemplate> <ItemsControl x:Name="PanelButtons" ItemsSource="{Binding ReachFloors}"> <ItemsControl.Template/> <ItemsControl.ItemsPanel/> <ItemsControl.ItemTemplate> <DataTemplate> <Button Content="{Binding .}" Command="{Binding ToFloorCmd, Source={StaticResource ELevator}}" > <Button.CommandParameter> <MultiBinding Converter="{StaticResource EBPC}" ConverterParameter="ParamTest"> <Binding Source="{StaticResource MyBuilding.Elevators}" Path="Number"/> <Binding Path="."/> </MultiBinding> </Button.CommandParameter> </Button> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>



问题就是Button的Command,这个Command希望将电梯号(在哪部电梯里的面板上按的)和目标楼层(想去哪层)两个参数传递到方法里
但由于命令的声明ToFloorCmd是写在Elevator类中了,因此在Button的父容器PanelButtons中的ItemsSource(ReachFloors)里是找不到这个命令的

所以这个Command的MultiBinding的Source必须指定成Elevators容器的ItemsSource(也就是MyBuilding.Elevators),可由于PanelButtons需要根据ReachFloors中数据条目的数量来决定生成多少个Button,而且Button自身也需要根据ReachFloors中的数据来显示自己的楼层号,所以PanelButtons的ItemsSource仍旧只能指定为(MyBuilding.Elavators.ReachFloors)

于是我只好放弃在.cs中声明Building实例,改为使用静态的资源字典,这样就可以在Command的MuiltiBinding中引用到和父容器的DataContext不同的Binding Source了。

如果是这个思路本身就有问题,能从这方面解决此问题也是可以的。

======

以上就是全部,希望能够得到诸位的帮助

如果有什么没说明白的地方请指出,我会尽量做出进一步的解释

非常感谢


...全文
34 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧