Skip to content

Instantly share code, notes, and snippets.

@NWoodsman
Last active April 15, 2022 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NWoodsman/eccbc3c3164bc0691c6841d2836e6684 to your computer and use it in GitHub Desktop.
Save NWoodsman/eccbc3c3164bc0691c6841d2836e6684 to your computer and use it in GitHub Desktop.
Bug Behavior System.Windows.Media.Pen
<Window
x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid Name="theGrid">
<Button
Width="100"
Height="40"
Click="Button_Click">
Change Pattern
</Button>
<TextBlock Name="dashText" Width="300" d:Text="test" TextAlignment="Center" VerticalAlignment="Center" Margin="0,-70,0,0"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
public class LineVisual : DrawingVisual
{
}
public class LineElement : UIElement
{
public static Pen Pen { get; set; } = new Pen(new SolidColorBrush(Colors.Black), 3);
private LineVisual LineVisual;
private LineGeometry lg { get; init; }
protected override Visual GetVisualChild(int index) => LineVisual;
protected override int VisualChildrenCount => 1;
public LineElement(Point start, Point end)
{
lg = new LineGeometry(start, end);
LineVisual = new LineVisual();
DrawingContext dc = LineVisual.RenderOpen();
dc.DrawGeometry(null, Pen, lg);
dc.Close();
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private int _counter = 0;
private int Counter
{
get
{
return _counter;
}
set
{
if(value == 5)
{
_counter = 0;
}
else
{
_counter = value;
}
}
}
private double Yval { get; set; } = 20;
private void Button_Click(object sender, RoutedEventArgs e)
{
switch (Counter)
{
case 0:
LineElement l1 = new LineElement(new Point(10, Yval), new Point(200, Yval));
theGrid.Children.Add(l1);
LineElement.Pen.DashStyle = DashStyles.Solid;
dashText.Text = LineElement.Pen.DashStyle.Dashes.ToString();
Yval += 20;
Counter++;
break;
case 1:
LineElement.Pen.DashStyle = DashStyles.Dash;
dashText.Text = LineElement.Pen.DashStyle.Dashes.ToString();
LineElement l2 = new LineElement(new Point(10,Yval), new Point(200,Yval));
theGrid.Children.Add(l2);
Yval += 20;
Counter++;
break;
case 2:
LineElement.Pen.DashStyle = DashStyles.Dot;
dashText.Text = LineElement.Pen.DashStyle.Dashes.ToString();
LineElement l3 = new LineElement(new Point(10, Yval), new Point(200, Yval));
theGrid.Children.Add(l3);
Yval += 20;
Counter++;
break;
case 3:
LineElement.Pen.DashStyle = DashStyles.DashDot;
dashText.Text = LineElement.Pen.DashStyle.Dashes.ToString();
LineElement l4 = new LineElement(new Point(10, Yval), new Point(200, Yval));
theGrid.Children.Add(l4);
Yval += 20;
Counter++;
break;
case 4:
LineElement.Pen.DashStyle = DashStyles.DashDotDot;
dashText.Text = LineElement.Pen.DashStyle.Dashes.ToString();
LineElement l5 = new LineElement(new Point(10, Yval), new Point(200, Yval));
theGrid.Children.Add(l5);
Yval += 20;
Counter++;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment