WPF 获取框选的控件 并把选定的控件用红色填充

已经实现鼠标按住左键不放 进行拖拽选定.
问:如何设置选定控件的背景色为红色
代码
<Grid Name="grid1" Margin="47,190,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="286" Width="777" MouseLeftButtonDown="grid1_MouseLeftButtonDown" MouseLeftButtonUp="grid1_MouseLeftButtonUp" MouseMove="grid1_MouseMove">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid Name="gridContainer1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="286" Width="777" MouseLeftButtonDown="grid1_MouseLeftButtonDown">
</Grid>
</Grid>
Line oneTotwo;
Line oneTothree;
Line twoTofour;
Line threeTofour;
int clicktimes = 0;
private void grid1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
int x = Convert.ToInt32(e.GetPosition(grid1).X);
int y = Convert.ToInt32(e.GetPosition(grid1).Y);
label2333.Content = "x:" + x + " y:" + y;
if (clicktimes > 0)
{
for (int i = 0; i < 4; i++)
{
grid1.Children.RemoveAt(0);
}
clicktimes = 0;
}
if (0 == clicktimes)
{
Point clickPoint = (Point)e.GetPosition(grid1);
oneTotwo = new Line();
oneTotwo.Stroke = Brushes.Red;
oneTotwo.Fill = Brushes.Red;
oneTotwo.StrokeLineJoin = PenLineJoin.Bevel;
oneTotwo.X1 = clickPoint.X;
oneTotwo.Y1 = clickPoint.Y;
oneTotwo.X2 = clickPoint.X + 10;
oneTotwo.Y2 = clickPoint.Y;
oneTotwo.StrokeThickness = 1;
grid1.Children.Insert(0,oneTotwo);
int zindex = grid1.Children.Count;
Grid.SetZIndex(oneTotwo, zindex);
oneTothree = new Line();
oneTothree.Stroke = Brushes.Red;
oneTothree.Fill = Brushes.Red;
oneTothree.StrokeLineJoin = PenLineJoin.Bevel;
oneTothree.X1 = clickPoint.X;
oneTothree.Y1 = clickPoint.Y;
oneTothree.X2 = clickPoint.X;
oneTothree.Y2 = clickPoint.Y + 10;
oneTothree.StrokeThickness = 1;
grid1.Children.Insert(1,oneTothree);
zindex = grid1.Children.Count;
Grid.SetZIndex(oneTothree, zindex);
twoTofour = new Line();
twoTofour.Stroke = Brushes.Red;
twoTofour.Fill = Brushes.Red;
twoTofour.StrokeLineJoin = PenLineJoin.Bevel;
twoTofour.X1 = clickPoint.X + 10;
twoTofour.Y1 = clickPoint.Y;
twoTofour.X2 = clickPoint.X + 10;
twoTofour.Y2 = clickPoint.Y + 10;
twoTofour.StrokeThickness = 1;
grid1.Children.Insert(2, twoTofour);
zindex = grid1.Children.Count;
Grid.SetZIndex(twoTofour, zindex);
threeTofour = new Line();
threeTofour.Stroke = Brushes.Red;
threeTofour.Fill = Brushes.Red;
threeTofour.StrokeLineJoin = PenLineJoin.Bevel;
threeTofour.X1 = clickPoint.X;
threeTofour.Y1 = clickPoint.Y + 10;
threeTofour.X2 = clickPoint.X + 10;
threeTofour.Y2 = clickPoint.Y + 10;
threeTofour.StrokeThickness = 1;
grid1.Children.Insert(3, threeTofour);
zindex = grid1.Children.Count;
Grid.SetZIndex(threeTofour, zindex);
clicktimes++;
}
}
private void grid1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
int x = Convert.ToInt32(e.GetPosition(grid1).X);
int y = Convert.ToInt32(e.GetPosition(grid1).Y);
label2333.Content = "离开位置: " + Convert.ToString(x) + "," + Convert.ToString(y);
oneTotwo = null;
oneTothree = null;
twoTofour = null;
threeTofour = null;
//获取已选定的表格
object item = e.GetPosition(gridContainer1);
//gridContainer1.RowDefinitions[0].
}
private void grid1_MouseMove(object sender, MouseEventArgs e)
{
Point drawPoint = (Point)e.GetPosition(grid1);
if (oneTotwo != null & twoTofour != null & oneTothree != null & threeTofour != null & e.LeftButton == MouseButtonState.Pressed)
{
twoTofour.X1 = oneTotwo.X2 = twoTofour.X2 = threeTofour.X2 = drawPoint.X;
threeTofour.Y1 = threeTofour.Y2 = twoTofour.Y2 = twoTofour.Y2 = oneTothree.Y2 = drawPoint.Y;
}
}相关推荐
用C# 实现鼠标框选效果的实现代码
实现步骤: 1.实现整个鼠标框选的几个事件(down、move、up),当鼠标点下记录鼠标框选的起点,鼠标抬起结束操作。 2.以鼠标框选过程中获取的鼠标坐标为基点计算框选的矩形的4点坐标,4点坐标以顺时针方向布点。 3.通过Shape.Path类实现在类上画出此矩形。 代码如下: 代码如下:namespace HostDemo { public class HostCanvas : Canvas { public HostCanvas() { InitializeComponent(); } private void InitializeComponent() { this.L
WPF获取系统时间
WPF获取系统时间,另外还有WPF复杂属性问题,事例:解决渐变色问题
WPF实现添加图片文本
用户控件,可以实现添加所选择的图标,图片,以及有格式的文本。
WPF经典教程之Canvas、InkCanvas布局
在WPF中子元素的绝对定位的布局控件 其子元素使用Width、Height定义元素的宽度和高度 使用Convas.Left(Convas.Right)、Convas.Top(Convas.Bottom)定义与Convas容器的相对位置..... (更多资源:http://cleopard.download.csdn.net/) (福利:http://xuemeilaile.com/) WPF经典教程之WPF体系结构 http://download.csdn.net/detail/cleopard/7999393 WPF经典教程之WPF应用程序管理 http://download.csdn.net/detail/cleopard/8002969 WPF经典教程之WPF窗体 http://download.csdn.net/detail/cleopard/8002979 WPF经典教程之StackPanel、WrapPanel、DockPanel布局 http://download.csdn.net/detail/cleopard/8002985 WPF经典教程之Grid、UniformGrid布局 http://download.csdn.net/detail/cleopard/8002993
wpf border 边框不同颜色
wpf border边框增加四个属性leftborderbrush.... 分别设置不同颜色


