private void InitDrawEquip(EquipType equipType) { if (drawItem != null) { canvas.Children.Remove(drawItem as UCEquipIcon); drawItem = null; } UCEquipIcon ucEquip = new UCEquipIcon(); Binding bindWidth = new Binding() { Path = new PropertyPath("UCEquipLength"), Source = this }; bindWidth.Converter = new PicWidthToUCWidthConverter(); Binding bindHeight = new Binding() { Path = new PropertyPath("UCEquipLength"), Source = this }; bindHeight.Converter = new PicHeightToUCHeightConverter(); ucEquip.SetBinding(UCEquipIcon.WidthProperty, bindWidth); ucEquip.SetBinding(UCEquipIcon.HeightProperty, bindHeight); Equipment equip = new Equipment(); ucEquip.Equip = equip; equip.Type = equipType; ucEquip.Visibility = Visibility.Hidden; drawItem = ucEquip; canvas.Children.Add(ucEquip as UCEquipIcon); }
/// <summary> /// 根据设备类信息和上个设备图标信息来生成本设备图标并定位 /// </summary> /// <param name="equip"></param> /// <param name="ucLastEquip"></param> /// <returns></returns> private UCEquipIcon GetUCEquipIcon(Equipment equip) { //将图标长宽绑定到程序设置的UCEquipLength变量上来,这个变量后面可以让用户调节 Binding bindWidth = new Binding() { Path = new PropertyPath("UCEquipLength"), Source = this }; bindWidth.Converter = new PicWidthToUCWidthConverter(); Binding bindHeight = new Binding() { Path = new PropertyPath("UCEquipLength"), Source = this }; bindHeight.Converter = new PicHeightToUCHeightConverter(); //图标控件的坐标绑定到设备类的坐标上 Binding bindX = new Binding { Path = new PropertyPath("X"), Source = equip, Mode = BindingMode.TwoWay }; Binding bindY = new Binding { Path = new PropertyPath("Y"), Source = equip, Mode = BindingMode.TwoWay }; //图标控件的中心点绑定到设备类的坐标上 Binding bindCenterX = new Binding { Path = new PropertyPath("X"), Source = equip }; bindCenterX.Converter = new PointToCenterPointConverter(); bindCenterX.ConverterParameter = ucEquipLength; Binding bindCenterY = new Binding { Path = new PropertyPath("Y"), Source = equip }; bindCenterY.Converter = new PointYToCenterPointYConverter(); bindCenterY.ConverterParameter = ucEquipLength; UCEquipIcon ucEquipIcon = null; this.Dispatcher.Invoke(new Action(() => { ucEquipIcon = new UCEquipIcon(); ucEquipIcon.SetBinding(UCEquipIcon.WidthProperty, bindWidth); ucEquipIcon.SetBinding(UCEquipIcon.HeightProperty, bindHeight); ucEquipIcon.SetBinding(Canvas.LeftProperty, bindX); ucEquipIcon.SetBinding(Canvas.TopProperty, bindY); ucEquipIcon.SetBinding(UCEquipIcon.CenterPointXProperty, bindCenterX); ucEquipIcon.SetBinding(UCEquipIcon.CenterPointYProperty, bindCenterY); canvas.Children.Add(ucEquipIcon); })); //图标控件和设备类互相引用 ucEquipIcon.Equip = equip; equip.UCEquipIcon = ucEquipIcon; //图标控件的属性和事件 ucEquipIcon.MouseLeftButtonDown += new MouseButtonEventHandler(ucEquip_MouseLeftButtonDown); ucEquipIcon.MouseMove += new MouseEventHandler(ucEquip_MouseMove); ucEquipIcon.MouseLeftButtonUp += new MouseButtonEventHandler(ucEquip_MouseLeftButtonUp); ucEquipIcon.MouseRightButtonDown += new MouseButtonEventHandler(ucEquip_MouseRightButtonDown); return(ucEquipIcon); }