Commonly used graphics operations implemented InkCanvas

Functions: 1, ink drawing, point to delete, delete the graphics, select ink graphics

     2, draw text, draw a rectangle, circle, ellipse, draw pictures, arrows drawn

     3, copy and paste function, undo function

     4, save ink, ink reading

Renderings:

Realization of ideas: involves customized ink drawing, such as arrows, pictures, need custom InkCanvas and Stroke, the preparation of mouse clicks to move down event logic

Custom Stroke

 

CustomStroke class public: the Stroke
{
/// <Summary>
/// 1 Text, Image 2
/// </ Summary>
public int {ShowType GET; SET;} = 0;
/// <Summary>
information image ///
/// </ Summary>
public the BitmapImage the ImageSource {GET; SET;}
/// <Summary>
/// text
/// </ Summary>
public String the content {GET; SET;}
/// <Summary>
/ font size //
/// </ Summary>
public Double the FontSize {GET; SET;}

 

public CustomStroke(StylusPointCollection stylusPoints, DrawingAttributes drawingAttributes)
: base(stylusPoints, drawingAttributes)
{

}
public override Stroke Clone()
{
CustomStroke customStroke = new CustomStroke(base.StylusPoints, base.DrawingAttributes);
customStroke.ShowType = ShowType;
customStroke.BitmapImage = BitmapImage;
customStroke.Content = Content;
customStroke.FontSize = FontSize;
return customStroke;
}

 

protected override void DrawCore(DrawingContext drawingContext,
DrawingAttributes drawingAttributes)
{
if(ShowType==0)
{
base.Draw(drawingContext, drawingAttributes);
return;
}
// Allocate memory to store the previous point to draw from.
Point prevPoint = new Point(double.NegativeInfinity,
double.NegativeInfinity);

 

// Draw linear gradient ellipses between 
// all the StylusPoints in the Stroke.
for (int i = 0; i < this.StylusPoints.Count; i++)
{
Point pt = (Point)this.StylusPoints[i];
Vector v = Point.Subtract(prevPoint, pt);

 

// Only draw if we are at least 4 units away 
// from the end of the last ellipse. Otherwise, 
// we're just redrawing and wasting cycles.
if (v.Length > 4)
{

 

if (ShowType == 1)
{
Label labelNew = new Label();
labelNew.Content = Content;
labelNew.FontSize = FontSize;
var formattedText = new FormattedText(labelNew.Content.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(labelNew.FontFamily, labelNew.FontStyle, labelNew.FontWeight, labelNew.FontStretch), labelNew.FontSize, Brushes.Black);
drawingContext.DrawText(formattedText, new Point(pt.X - formattedText.Width / 2, pt.Y - formattedText.Height / 2));
this.DrawingAttributes.Height = formattedText.Height;
this.DrawingAttributes.Width = formattedText.Width;
}
else
{
drawingContext.DrawImage(BitmapImage, new Rect(pt.X - 10, pt.Y - 10, 20, 20));

 

}

 


}
}
}
}
Public enum ShowType
{
/// <Summary>
/// cancel
/// </ Summary>
None,
/// <Summary>
/// drawing (pen)
/// </ Summary>
Ink,
/// <Summary>
/// eraser (point rub)
/// </ Summary>
EraseByPoint,
/// <Summary>
/// wiping pattern (line rub)
/// </ Summary>
EraseByStroke,
// / <Summary>
/// selected pattern
/// </ Summary>
the select,
/// <Summary>
/// input text
/// </ Summary>
the text,
/// <Summary>
/// drawing a rectangle
/ // </ Summary>
the Rectangle,
/// <Summary>
// draw the circle
/// </ summary>
Round,
/// <Summary>
/// drawing an elliptical
/// </ Summary>
Ellipse,
/// <Summary>
/// draw straight
/// </ Summary>
Line,
/// <Summary>
// / draw horizontal arrow
/// </ Summary>
LineArrowH,
/// <Summary>
/// arrow drawn longitudinal
/// </ Summary>
LineArrowV,
/// <Summary>
/// horizontal arrow drawn (mono)
/ // </ Summary>
LineArrowHOne,
/// <Summary>
/// arrow longitudinal drawing (single)
/// </ summary>
LineArrowVOne,
/// <Summary>
/// graphing
/// </ summary>
Image

 

}

 

Custom InkCanvas

 

public class CustomRenderingInkCanvas : InkCanvas
{
private List<Dictionary<int, List<Stroke>>> listUndoStroke = new List<Dictionary<int, List<Stroke>>>();
public CustomRenderingInkCanvas() : base()
{
this.PreviewKeyDown += CustomRenderingInkCanvas_PreviewKeyDown;
this.DefaultDrawingAttributes.Width = 1;
}
/// <summary>
/// 加入撤销清单
/// </summary>
/// <param name="stroke"></param>
/// <param name="type">(0新增,1删除)</param>
public void AddUndoStroke(Stroke stroke,int type)
{
if(listUndoStroke.Count==10)
{
listUndoStroke.RemoveAt(0);
}
listUndoStroke.Add(new Dictionary<int, List<Stroke>>() { { type, new List<Stroke>() { stroke } } });
}
/// <summary>
/// 加入撤销清单
/// </summary>
/// <param name="stroke"></param>
/// <param name="type">(0新增,1删除)</param>
public void AddUndoStroke(List<Stroke> strokes, int type)
{
if (listUndoStroke.Count == 10)
{
listUndoStroke.RemoveAt(0);
}
listUndoStroke.Add(new Dictionary<int, List<Stroke>>() { { type, strokes } });
}

 

/// <summary>
/// 撤销操作
/// </summary>
public void UndoStroke()
{
if(listUndoStroke.Count != 0)
{
Dictionary<int, List<Stroke>> dicStroke = listUndoStroke[listUndoStroke.Count - 1];
int type=0;
List <Stroke> strokes=new List<Stroke>();
foreach (var key in dicStroke.Keys)
{
type = key;
strokes = dicStroke[key];
}
if(type==0) //新增做删除
{
foreach (var stroke in strokes)
{
this.Strokes.Remove(stroke);
}
}else
{
foreach (var stroke in strokes)
{
this.Strokes.Add(stroke);
}
}
listUndoStroke.Remove(dicStroke);
}
}
StrokeCollection strokesSelect;
private void CustomRenderingInkCanvas_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
e.Handled = true;
if (this.GetSelectedStrokes().Count > 0)
{
List<Stroke> listUndo = new List<Stroke>();
foreach (var stroke in this.GetSelectedStrokes())
{
listUndo.Add(stroke);
this.Strokes.Remove(stroke);
}
AddUndoStroke(listUndo, 1);
}

}
else if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
{
strokesSelect = this.GetSelectedStrokes();
e.Handled = true;
}
else if (e.Key == Key.V && Keyboard.Modifiers == ModifierKeys.Control)
{
if (strokesSelect != null&& strokesSelect.Count>0)
{
//找到最小坐标
double minXSelect = double.MaxValue;
double minYSelect = double.MaxValue;
foreach (Stroke stroke in strokesSelect)
{
double minX = stroke.StylusPoints.Min(it => it.X);
double minY = stroke.StylusPoints.Min(it => it.Y);
if (minX < minXSelect)
{
minXSelect = minX;
}
if (minY < minYSelect)
{
minYSelect = minY;
}

}
Point point = Mouse.GetPosition(this);
double mouseX = point.X - minXSelect;
double mouseY = point.Y - minYSelect;
List<Stroke> listUndo = new List<Stroke>();
foreach (Stroke stroke in strokesSelect)
{
Stroke strokeNew = stroke.Clone();
StylusPointCollection stylusPointsNew = new StylusPointCollection();
foreach (var stylusPoint in stroke.StylusPoints)
{
stylusPointsNew.Add(new StylusPoint(stylusPoint.X + mouseX, stylusPoint.Y + mouseY, stylusPoint.PressureFactor)); 
}
strokeNew.StylusPoints= stylusPointsNew;
this.Strokes.Add(strokeNew);
listUndo.Add(strokeNew);
}
AddUndoStroke(listUndo, 0);

 

e.Handled = true;
}
}
}
protected override void OnSelectionChanging(InkCanvasSelectionChangingEventArgs e)
{
if(e.GetSelectedElements().Count>0)
{
e.Cancel = true;
}
}

 

protected override void OnStrokeCollected(InkCanvasStrokeCollectedEventArgs e)
{
base.OnStrokeCollected(e);
AddUndoStroke(e.Stroke, 0);
}
protected override void OnStrokeErasing(InkCanvasStrokeErasingEventArgs e)
{
base.OnStrokeErasing(e);
AddUndoStroke(e.Stroke, 1);
}

 

}

The key code page (due to the use of the resource picture, just put some of the key codes for reference) because with custom Stroks, you can not use InkCanvas own preservation methods, the need to resolve their own (pit has stepped)

/// <summary>
/// 加载墨迹Json
/// </summary>
/// <param name="json"></param>
private void LoadJson(string json,CustomRenderingInkCanvas canvas)
{

foreach (var stroke in JsonHelper.ToObject<List<object>>(json))
{
DrawingAttributes drawingAttributes = JsonHelper.ToObject<DrawingAttributes>(JsonHelper.ReadJsonString(stroke.ToString(), "DrawingAttributes"));
StylusPointCollection stylusPoints = JsonHelper.ToObject<StylusPointCollection>(JsonHelper.ReadJsonString(stroke.ToString(), "StylusPoints"));
if (!string.IsNullOrEmpty(JsonHelper.ReadJsonString(stroke.ToString(), "ShowType"))) // custom ink CustomStroke strokeNew = new new CustomStroke (the StylusPoints, DrawingAttributes);
{

strokeNew.ShowType = Convert.ToInt32(JsonHelper.ReadJsonString(stroke.ToString(), "ShowType"));
strokeNew.Content = JsonHelper.ReadJsonString(stroke.ToString(), "Content");
strokeNew.FontSize = Convert.ToDouble(JsonHelper.ReadJsonString(stroke.ToString(), "FontSize"));
if (!string.IsNullOrEmpty(JsonHelper.ReadJsonString(stroke.ToString(), "BitmapImage")))
strokeNew.BitmapImage = new BitmapImage(new Uri(JsonHelper.ReadJsonString(stroke.ToString(), "BitmapImage")));
canvas.Strokes.Add(strokeNew);
}
else
{
canvas.Strokes.Add(new Stroke(stylusPoints, drawingAttributes));
}
}
}
/// <summary>
/// 保存墨迹Json
/// </summary>
public bool SaveImageJson()
{
try
{

if (File.Exists(localSheet))
{
string json = JsonHelper.ToJson(this.inkCanvasSheet.Strokes);
File.WriteAllText(localSheetJson, json);
}
if (File.Exists(localPanelA))
{
string json = JsonHelper.ToJson(this.inkCanvasPanelA.Strokes);
File.WriteAllText(localPanelAJson, json);
}
if (File.Exists(localPanelB))
{
string json = JsonHelper.ToJson(this.inkCanvasPanelB.Strokes);
File.WriteAllText(localPanelBJson, json);
}
return true;
}
catch (Exception ex)
{
return false;
}
}

 

private void CustomRenderingInkCanvas_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (strokeMove != null)
{
CustomRenderingInkCanvas inkCanvas = sender as CustomRenderingInkCanvas;
inkCanvas.AddUndoStroke(strokeMove, 0);
strokeMove = null;
}
}

Stroke strokeMove;
private void CustomRenderingInkCanvas_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
CustomRenderingInkCanvas inkCanvas = sender as CustomRenderingInkCanvas;
Point pointEnd = e.GetPosition(inkCanvas);
if (showType == ShowType.Rectangle)
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointStart.X, pointEnd.Y),
new Point(pointEnd.X, pointEnd.Y),
new Point(pointEnd.X, pointStart.Y),
new Point(pointStart.X, pointStart.Y),
};
AddCustomStorke(pointList, inkCanvas);
}
else if (showType == ShowType.Ellipse)
{
List<Point> pointList = GenerateEclipseGeometry(pointStart, pointEnd);
AddCustomStorke(pointList, inkCanvas);
}
else if (showType == ShowType.Round)
{
List<Point> pointList = GenerateRoundGeometry(pointStart, pointEnd);
AddCustomStorke(pointList, inkCanvas);
}
else if (showType == ShowType.Line)
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointEnd.X, pointEnd.Y)
};
AddCustomStorke(pointList, inkCanvas);
}
else if (showType == ShowType.LineArrowH)//横向箭头
{
if (pointEnd.X < pointStart.X)
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointEnd.X, pointStart.Y),
new Point(pointEnd.X+10, pointStart.Y+5),
new Point(pointEnd.X, pointStart.Y),
new Point(pointEnd.X+10, pointStart.Y-5)
};
AddCustomStorke(pointList, inkCanvas);
}
else
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointEnd.X, pointStart.Y),
new Point(pointEnd.X-10, pointStart.Y-5),
new Point(pointEnd.X, pointStart.Y),
new Point(pointEnd.X-10, pointStart.Y+5)
};
AddCustomStorke(pointList, inkCanvas);
}
}
else if (showType == ShowType.LineArrowHOne)//横向箭头单
{
if (pointEnd.X < pointStart.X)
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointEnd.X, pointStart.Y),
new Point(pointEnd.X+10, pointStart.Y+5)
};
AddCustomStorke(pointList, inkCanvas);
}
else
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointEnd.X, pointStart.Y),
new Point(pointEnd.X-10, pointStart.Y+5)
};
AddCustomStorke(pointList, inkCanvas);
}
}
else if (showType == ShowType.LineArrowV)//纵向箭头
{
if (pointEnd.Y < pointStart.Y)
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointStart.X, pointEnd.Y),
new Point(pointStart.X+5, pointEnd.Y+10),
new Point(pointStart.X, pointEnd.Y),
new Point(pointStart.X-5, pointEnd.Y+10)
};
AddCustomStorke(pointList, inkCanvas);
}
else
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointStart.X, pointEnd.Y),
new Point(pointStart.X+5, pointEnd.Y-10),
new Point(pointStart.X, pointEnd.Y),
new Point(pointStart.X-5, pointEnd.Y-10)
};
AddCustomStorke(pointList, inkCanvas);
}
}
else if (showType == ShowType.LineArrowVOne)//纵向箭头(单)
{
if (pointEnd.Y < pointStart.Y)
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointStart.X, pointEnd.Y),
new Point(pointStart.X+5, pointEnd.Y+10)
};
AddCustomStorke(pointList, inkCanvas);
}
else
{
List<Point> pointList = new List<Point>
{
new Point(pointStart.X, pointStart.Y),
new Point(pointStart.X, pointEnd.Y),
new Point(pointStart.X+5, pointEnd.Y-10)
};
AddCustomStorke(pointList, inkCanvas);
}
}
}
}
private void AddCustomStorke(List<Point> pointList, CustomRenderingInkCanvas inkCanvas)
{
StylusPointCollection point = new StylusPointCollection(pointList);
Stroke stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
if (strokeMove != null)
{
inkCanvas.Strokes.Remove(strokeMove);
}
inkCanvas.Strokes.Add(stroke);
strokeMove = stroke;
}
private void CustomRenderingInkCanvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
//本身操作不执行
if(showType == ShowType.None|| showType == ShowType.Ink || showType == ShowType.EraseByPoint || showType == ShowType.EraseByStroke || showType == ShowType.Select)
{
return;
}
CustomRenderingInkCanvas inkCanvas = sender as CustomRenderingInkCanvas;
StylusPointCollection stylusPoints = new StylusPointCollection();
pointStart = e.GetPosition(inkCanvas);
stylusPoints.Add(new StylusPoint(pointStart.X, pointStart.Y));
CustomStroke customStroke = new CustomStroke(stylusPoints,new DrawingAttributes());
if (showType == ShowType.Image)
{
customStroke.ShowType = 2;
customStroke.BitmapImage = imageSourceSelect;
inkCanvas.Strokes.Add(customStroke);
inkCanvas.AddUndoStroke(customStroke, 0);
}
else if(showType==ShowType.Text)
{
if(string.IsNullOrEmpty(tboxText.Text))
{
VicMessageBoxNormal.Show("请输入文字!");
return;
}
customStroke.ShowType = 1;
customStroke.Content = tboxText.Text;
customStroke.FontSize = (double)numFontSize.Value;
inkCanvas.Strokes.Add(customStroke);
inkCanvas.AddUndoStroke(customStroke, 0);
}

}
private List<Point> GenerateEclipseGeometry(Point st, Point ed)
{
double a = 0.5 * (ed.X - st.X);
double b = 0.5 * (ed.Y - st.Y);
List<Point> pointList = new List<Point>();
for (double r = 0; r <= 2 * Math.PI; r = r + 0.01)
{
pointList.Add(new Point(0.5 * (st.X + ed.X) + a * Math.Cos(r), 0.5 * (st.Y + ed.Y) + b * Math.Sin(r)));
}
return pointList;
}
private List<Point> GenerateRoundGeometry(Point st, Point ed)
{
double rLength = Math.Abs(ed.X - st.X) > Math.Abs(ed.Y - st.Y) ? Math.Abs(ed.X - st.X) : Math.Abs(ed.Y - st.Y);
Point pointStart = new Point(st.X - rLength, st.Y - rLength);
Point pointEnd = new Point(st.X + rLength, st.Y + rLength);
double a = 0.5 * (pointEnd.X - pointStart.X);
double b = 0.5 * (pointEnd.Y - pointStart.Y);
List<Point> pointList = new List<Point>();
for (double r = 0; r <= 2 * Math.PI; r = r + 0.01)
{
pointList.Add(new Point(0.5 * (pointStart.X + pointEnd.X) + a * Math.Cos(r), 0.5 * (pointStart.Y + pointEnd.Y) + b * Math.Sin(r)));
}
return pointList;
}
private Point pointStart;
private void VicRadioButtonNormal_Click(object sender, RoutedEventArgs e)
{
SetInkCanvasState(InkCanvasEditingMode.None);
VicRadioButtonNormal radioButtonNormal = sender as VicRadioButtonNormal;
switch(radioButtonNormal.ToolTip)
{
case "输入文字":
showType = ShowType.Text;
break;
case "取消操作":
showType = ShowType.None;
break;
case "特殊符号":
showType = ShowType.Image;
Image image = (Image)radioButtonNormal.Template.FindName("image1", radioButtonNormal);
imageSourceSelect = image.Source;
break;
case "选择图形":
showType = ShowType.Select;
SetInkCanvasState(InkCanvasEditingMode.Select);
break;
case "橡皮檫":
showType = ShowType.EraseByPoint;
SetInkCanvasState(InkCanvasEditingMode.EraseByPoint);
break;
case "删除图形":
showType = ShowType.EraseByStroke;
SetInkCanvasState(InkCanvasEditingMode.EraseByStroke);
break;
case "画笔":
showType = ShowType.Ink;
SetInkCanvasState(InkCanvasEditingMode.Ink);
break;
case "绘制矩形":
showType = ShowType.Rectangle;
//logic
BREAK;
Case "draw a line":
ShowType = ShowType.Line;
// logic
BREAK;
Case "draw a circle":
ShowType = ShowType.Round;
// logic
BREAK;
Case "draw an ellipse":
ShowType = ShowType.Ellipse;
// logic
BREAK;
Case "horizontal arrow Videos":
ShowType = ShowType.LineArrowH;
// logic
BREAK;
Case "vertical arrow Videos":
ShowType = ShowType.LineArrowV;
// logic
BREAK;
Case "arrow cross-Videos (single)":
ShowType ShowType.LineArrowHOne =;
// logic
BREAK;
Case "Videos arrow horizontal (vertical)":
ShowType = ShowType.LineArrowVOne;
// logic
BREAK;
}
}
/// <Summary>
/// set the canvas editable
/// </ summary>
/// <param name="editingMode"></param>
private void SetInkCanvasState(InkCanvasEditingMode editingMode)
{
inkCanvasSheet.EditingMode = editingMode;
inkCanvasPanelA.EditingMode = editingMode;
inkCanvasPanelB.EditingMode = editingMode;
}

private void btnCancel_Click(object sender, RoutedEventArgs e)
{
if (inkCanvasSheet.Visibility == Visibility.Visible)
{
inkCanvasSheet.UndoStroke();
}else if (inkCanvasPanelA.Visibility == Visibility.Visible)
{
inkCanvasPanelA.UndoStroke();
}
else if (inkCanvasPanelB.Visibility == Visibility.Visible)
{
inkCanvasPanelB.UndoStroke();
}
}

Xaml partial layout distal Code


<Window.Resources>
<converters:ImageSourceConveter x:Key="imageSourceConveter"/>

<Style x:Key="radioButtonTab" TargetType="RadioButton">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="MinWidth" Value="60"/>
<Setter Property="IsThreeState" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Border BorderThickness="1" x:Name="border" BorderBrush="#B9B9B9" Background="{TemplateBinding Background}" VerticalAlignment="Center">
<Label Content="{TemplateBinding Content}"/>

</Border>
<!--触发器:设置选中状态符号-->
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Background" Value="#CCEBF8"></Setter>
<Setter TargetName="border" Property="BorderBrush" Value="#199ED8"></Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#9DE0FC"></Setter>
<Setter TargetName="border" Property="BorderBrush" Value="#199ED8"></Setter>
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="radioButton" TargetType="RadioButton">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="IsThreeState" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Border BorderThickness="1" x:Name="border" Width="30" Height="30" BorderBrush="#B9B9B9" Background="{TemplateBinding Background}" VerticalAlignment="Center">
<Image x:Name="image1" Width="25" Height="25" Source="{TemplateBinding Content,Converter={StaticResource imageSourceConveter}}"/>

</Border>
<!--触发器:设置选中状态符号-->
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Background" Value="#CCEBF8"></Setter>
<Setter TargetName="border" Property="BorderBrush" Value="#199ED8"></Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#9DE0FC"></Setter>
<Setter TargetName="border" Property="BorderBrush" Value="#199ED8"></Setter>
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="button" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderThickness="1" x:Name="border" Width="40" Height="40" BorderBrush="#B9B9B9" Background="{TemplateBinding Background}" VerticalAlignment="Center">
<Image x:Name="image1" Width="25" Height="25" Source="{TemplateBinding Content,Converter={StaticResource imageSourceConveter}}"/>

</Border>
<!--触发器:设置选中状态符号-->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#9DE0FC"></Setter>
<Setter TargetName="border" Property="BorderBrush" Value="#199ED8"></Setter>
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<controls:VicWrapPanelNormal Grid.Column="0" Grid.RowSpan="3" Margin="5" Width="60">
<controls:VicRadioButtonNormal Style = "{StaticResource radioButton}" ToolTip = "text input" Content = "Entering Text .bmp" Click = "VicRadioButtonNormal_Click" />
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " cancel" Content = "cancel .bmp" the Click = "VicRadioButtonNormal_Click" />
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " drawing a rectangle "Content =" drawing a rectangle .bmp "the Click =" VicRadioButtonNormal_Click "/>
<Controls: VicRadioButtonNormal Style =" {} the StaticResource radioButton "the ToolTip =" eraser "Content =" eraser .bmp "the Click =" VicRadioButtonNormal_Click "/>
< controls: VicRadioButtonNormal Style = "{StaticResource radioButton}" ToolTip = " draw a straight line" Content = "draw a straight line .bmp" the Click = "VicRadioButtonNormal_Click" />
<Controls: VicRadioButtonNormal = Style "{StaticResource radioButton} "ToolTip =" delete pattern "Content =" Delete pattern .bmp "Click =" VicRadioButtonNormal_Click "/>
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " draw a circle" Content = "draw a circle .bmp" the Click = "VicRadioButtonNormal_Click" />
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " selection pattern "Content =" select graphic .bmp "the Click =" VicRadioButtonNormal_Click "/>
<Controls: VicRadioButtonNormal Style =" {} the StaticResource radioButton "the ToolTip =" draw an ellipse "Content =" draw an ellipse .bmp "the Click =" VicRadioButtonNormal_Click "/>
< controls: VicRadioButtonNormal Style = "{StaticResource radioButton}" ToolTip = " brush" Content = "brush .bmp" the Click = "VicRadioButtonNormal_Click" />
<Controls: VicButtonNormal Style = "{} the StaticResource Button"ToolTip="取消" Content="取消.bmp" x:Name="btnCancel" Click="btnCancel_Click"/>

<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " Videos horizontal arrow" Content = "Videos arrow .bmp" the Click = "VicRadioButtonNormal_Click" />
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " Videos vertical arrow "Content =" Videos arrow 2.bmp "the Click =" VicRadioButtonNormal_Click "/>
<Controls: VicRadioButtonNormal Style =" {} the StaticResource radioButton "the ToolTip =" arrow cross-Videos (single) "Content =" Videos arrow 3.bmp " = the Click "VicRadioButtonNormal_Click" />
<Controls: VicRadioButtonNormal Style = "{} the StaticResource radioButton" the ToolTip = "Videos arrow horizontal (vertical)" Content = "Videos arrow 4.bmp" the Click = "VicRadioButtonNormal_Click" />
<Controls: Style VicRadioButtonNormal = "{StaticResource radioButton} "ToolTip =" special symbols "Content =" special symbol insertion 16.bmp "Click =" VicRadioButtonNormal_Click "/>
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " special symbols" Content = "special symbol insertion 17.bmp" the Click = "VicRadioButtonNormal_Click" />
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " special symbol "Content =" special symbol insertion 18.bmp "the Click =" VicRadioButtonNormal_Click "/>
<Controls: VicRadioButtonNormal Style =" {} the StaticResource radioButton "the ToolTip =" special symbols "Content =" special symbol insertion 19.bmp "Click = "VicRadioButtonNormal_Click" />
<Controls: VicRadioButtonNormal Style = "{} the StaticResource radioButton" the ToolTip = "special symbols" Content = "special symbol insertion 20.bmp" the Click = "VicRadioButtonNormal_Click" />
<Controls: VicRadioButtonNormal = Style "{StaticResource radioButton} "ToolTip =" special symbols "Content =" special symbol insertion 21.bmp "Click =" VicRadioButtonNormal_Click "/>
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " special symbols" Content = "special symbol insertion 22.bmp" the Click = "VicRadioButtonNormal_Click" />
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " special symbol "Content =" special symbol insertion 23.bmp "the Click =" VicRadioButtonNormal_Click "/>
<Controls: VicRadioButtonNormal Style =" {} the StaticResource radioButton "the ToolTip =" special symbols "Content =" special symbol insertion .bmp "Click =" VicRadioButtonNormal_Click "/>
<Controls: VicRadioButtonNormal Style =" {} the StaticResource radioButton "the ToolTip =" special symbols "Content =" special symbol insertion 2.bmp "the Click =" VicRadioButtonNormal_Click "/>
<Controls: VicRadioButtonNormal = Style"{StaticResource radioButton} "ToolTip =" special symbols "Content =" special symbol insertion 3.bmp "Click =" VicRadioButtonNormal_Click "/>
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " special symbols" Content = "special symbol insertion 4.bmp" the Click = "VicRadioButtonNormal_Click" />
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " special symbol "Content =" special symbol insertion 5.bmp "the Click =" VicRadioButtonNormal_Click "/>
<Controls: VicRadioButtonNormal Style =" {} the StaticResource radioButton "the ToolTip =" special symbols "Content =" special symbol insertion 6.bmp "Click = "VicRadioButtonNormal_Click" />
<Controls: VicRadioButtonNormal Style = "{} the StaticResource radioButton" the ToolTip = "special symbols" Content = "special symbol insertion 7.bmp" the Click = "VicRadioButtonNormal_Click" />
<Controls: VicRadioButtonNormal = Style "{StaticResource radioButton} "ToolTip =" special symbols "Content =" special symbol insertion 8.bmp "Click =" VicRadioButtonNormal_Click "/>
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " special symbols" Content = "special symbol insertion 9.bmp" the Click = "VicRadioButtonNormal_Click" />
<controls: VicRadioButtonNormal Style = "{ StaticResource radioButton}" ToolTip = " special symbol "Content =" special symbol insertion 10.bmp "the Click =" VicRadioButtonNormal_Click "/>
<Controls: VicRadioButtonNormal Style =" {} the StaticResource radioButton "the ToolTip =" special symbols "Content =" special symbol insertion 11.bmp "Click = "VicRadioButtonNormal_Click" />
<Controls: VicRadioButtonNormal Style = "{} the StaticResource radioButton" the ToolTip = "special symbols" Content = "special symbol insertion 12.bmp" the Click = "VicRadioButtonNormal_Click" />
<Controls: VicRadioButtonNormal = Style "{StaticResource radioButton} "ToolTip =" special symbols "Content =" special symbol insertion 13.bmp "Click =" VicRadioButtonNormal_Click "/>
<controls:VicRadioButtonNormal Style="{StaticResource radioButton}" ToolTip="特殊符号" Content="插入特殊符号14.bmp" Click="VicRadioButtonNormal_Click"/>
<controls:VicRadioButtonNormal Style="{StaticResource radioButton}" ToolTip="特殊符号" Content="插入特殊符号15.bmp" Click="VicRadioButtonNormal_Click"/>
</controls:VicWrapPanelNormal>

<controls:VicStackPanelNormal Orientation="Horizontal" Grid.Column="1" Grid.Row="0" Margin="5">
<controls:VicLabelNormal Content="笔画:"/>
<controls:VicNumericUpDown x:Name="numWidth" ValueChanged="numWidth_ValueChanged" Minimum="1" Maximum="50" Value="1" Margin="0,5,5,5" NumKind="IntegerNum"/>
<controls:VicLabelNormal Content="文字:"/>
<controls:VicTextBoxNormal x:Name="tboxText" Width="120" Margin="0,5,5,5"/>
<controls:VicLabelNormal Content="字体:"/>
<controls:VicNumericUpDown x:Name="numFontSize" Minimum="6" Maximum="72" Value="12" NumKind="IntegerNum"/>
</controls:VicStackPanelNormal>

<controls:VicStackPanelNormal Orientation="Horizontal" Grid.Column="1" Grid.Row="1">
<controls:VicRadioButtonNormal Style="{StaticResource radioButtonTab}" Content="Sheet图" IsChecked="True" Name="rbtnSheet" Click="rbtnSheet_Click"/>
<controls:VicRadioButtonNormal Style="{StaticResource radioButtonTab}" Content="Panel A" Name="rbtnPanelA" Click="rbtnPanelA_Click" />
<controls:VicRadioButtonNormal Style="{StaticResource radioButtonTab}" Content="Panel B" Name="rbtnPanelB" Click="rbtnPanelB_Click"/>
</controls:VicStackPanelNormal>
<local:CustomRenderingInkCanvas x:Name="inkCanvasSheet" Grid.Column="1" Grid.Row="2">
<controls:VicImageNormal x:Name="imageSheet" Width="{Binding ElementName=inkCanvasSheet,Path=ActualWidth}" Height="{Binding ElementName=inkCanvasSheet,Path=ActualHeight}"/>
</local:CustomRenderingInkCanvas>


<local:CustomRenderingInkCanvas x:Name="inkCanvasPanelA" Grid.Column="1" Grid.Row="2" Visibility="Hidden">
<controls:VicImageNormal x:Name="imagePanelA" Width="{Binding ElementName=inkCanvasPanelA,Path=ActualWidth}" Height="{Binding ElementName=inkCanvasPanelA,Path=ActualHeight}"/>
</local:CustomRenderingInkCanvas>
<local:CustomRenderingInkCanvas x:Name="inkCanvasPanelB" Grid.Column="1" Grid.Row="2" Visibility="Hidden">
<controls:VicImageNormal x:Name="imagePanelB" Width="{Binding ElementName=inkCanvasPanelB,Path=ActualWidth}" Height="{Binding ElementName=inkCanvasPanelB,Path=ActualHeight}"/>
</local:CustomRenderingInkCanvas>

</Grid>

 

trigger

 

public class ImageSourceConveter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ImageSource source = new BitmapImage();
try
{
if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
{
ImageSource imageSource = new BitmapImage(new Uri("pack://application:,,,/ShowDemo;Component/Images/" + value.ToString()));
return imageSource;
}
else
{
return source;
}
}
catch (Exception ex)
{
return source;
}
}

 

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}

 

Guess you like

Origin www.cnblogs.com/xiaoq-blog/p/12237464.html