/// <summary> /// 单击Grid快捷菜单项 /// </summary> private void Grid_MenuItem_Click(object sender, RoutedEventArgs e) { Point MIPoint = gridCM.TranslatePoint(new Point(0, 0), expressGrid); LabelModel labelInfo = new LabelModel() { 文本 = ((MenuItem)sender).Header.ToString(), 字体 = "宋体", 字号 = 16 * zoomCoefficient, 水平边距 = MIPoint.X, 垂直边距 = MIPoint.Y, 绝对定位 = true }; AddTextBlock(labelInfo, false); }
/// <summary> /// 向expressGrid添加TextBlock /// </summary> /// <param name="labelInfo">要添加的标签信息</param> /// <param name="isZoom">是否执行缩放计算</param> private void AddTextBlock(LabelModel labelInfo, bool isZoom) { if (labelInfo.文本 == null) { return; } string textString = labelInfo.文本.Trim(); if (textString.Length == 0) { return; } else { //初始化TextBlock,设置必要的相同属性 TextBlock elementTB = new TextBlock() { Cursor = Cursors.Hand, FontFamily = new FontFamily(labelInfo.字体), HorizontalAlignment = HorizontalAlignment.Left, MaxWidth = 288 * zoomCoefficient, TextWrapping = TextWrapping.Wrap, VerticalAlignment = VerticalAlignment.Top, Background = Brushes.Violet, //Background = new SolidColorBrush(Color.FromArgb(0x50, 0xff, 0, 0xff)), ContextMenu = textCM }; if (isZoom) { elementTB.FontSize = labelInfo.字号 * zoomCoefficient; elementTB.Margin = new Thickness(labelInfo.水平边距 * zoomCoefficient, labelInfo.垂直边距 * zoomCoefficient, 0, 0); } else { elementTB.FontSize = labelInfo.字号; elementTB.Margin = new Thickness(labelInfo.水平边距, labelInfo.垂直边距, 0, 0); } elementTB.MouseLeftButtonDown += TextBlock_MouseLeftButtonDown; elementTB.MouseLeftButtonUp += TextBlock_MouseLeftButtonUp; elementTB.MouseRightButtonUp += TextBlock_MouseRightButtonUp; //绑定联系人数据源 if (isBinding) { //替换标签用户信息 textString = Other.ReplaceLabel(textString); //需要绑定的联系人数据源 Binding binding; switch (textString) { case "[联系人]": binding = new Binding("名称"); BindingOperations.SetBinding(elementTB, TextBlock.TextProperty, binding); break; case "[昵称]": binding = new Binding("昵称"); BindingOperations.SetBinding(elementTB, TextBlock.TextProperty, binding); break; case "[公司名称]": binding = new Binding("显示公司"); BindingOperations.SetBinding(elementTB, TextBlock.TextProperty, binding); break; case "[公司简称]": binding = new Binding("公司简称"); BindingOperations.SetBinding(elementTB, TextBlock.TextProperty, binding); break; case "[地址]": binding = new Binding("地址"); BindingOperations.SetBinding(elementTB, TextBlock.TextProperty, binding); break; case "[手机]": binding = new Binding("手机优先"); BindingOperations.SetBinding(elementTB, TextBlock.TextProperty, binding); break; case "[电话]": binding = new Binding("电话优先"); BindingOperations.SetBinding(elementTB, TextBlock.TextProperty, binding); break; case "[传真]": binding = new Binding("传真优先"); BindingOperations.SetBinding(elementTB, TextBlock.TextProperty, binding); break; case "[日期]": elementTB.Text = DateTime.Now.ToShortDateString(); break; default: elementTB.Text = textString; break; } } //向expressGrid添加此元素 expressGrid.Children.Add(elementTB); } }