I want to add a number of canvas to another canvas but the following code doesn't seem to work, the application compiles but only a blank window is displayed. Ideally this would show the canvases in a diagonal line.
Here is my vb.net code:
For r As Integer = 1 To 10
X += 5
Y += 5
Dim c As Canvas = New Canvas()
Dim s As SolidColorBrush = New SolidColorBrush
s.Color = Color.FromRgb(255, 0, 0)
c.Background = s
c.Name = "cnv" + CStr(X)
c.Margin = New Thickness(X, Y, 0, 0)
cnvOverLay.Children.Add(c)
cnvOverLay.UpdateLayout()
Next
I have tried to add multiple buttons using dim b As Button = new Button() instead of creating a new canvas every time, that worked fine.
Am I missing something simple?
You cannot see the canvases be because the do not have a width and a height.
Related
I need to show/hide some data points in oxyplot line series. Is it possible?
Though some markers are invisible, the line should go through the invisible markers.
You could make use of two series to achieve this. The first one would draw the complete points (and line) without the marker. The second series would draw the visible points(with marker,but with line style set to none). For Example
DataPoint[] points = new DataPoint[]
{
new DataPoint(1,12),
new DataPoint(2,10),
new DataPoint(3,9),
new DataPoint(4,13),
new DataPoint(5,14),
new DataPoint(6,10)
};
var seriesComplete = new OxyPlot.Series.LineSeries();
seriesComplete.Points.AddRange(points);
var seriesVisible = new OxyPlot.Series.LineSeries();
seriesVisible.Points.AddRange(points.Where(x => x.Y % 2 == 0));
seriesVisible.MarkerFill = OxyColors.Blue;
seriesVisible.MarkerType = MarkerType.Circle;
seriesVisible.MarkerSize = 10;
seriesVisible.LineStyle = LineStyle.None;
this.MyModel.Series.Add(seriesComplete);
this.MyModel.Series.Add(seriesVisible);
Result is attached as image
I'm using the DataVisualization charting within WPF and creating a BarSeries in code, but I can't get the numbers formatted on the X axis.
I don't want to do this in XAML because the type of chart can vary according to user selection and it seemed more flexible to do all of this in code rather than having different chart types. The number of data series also fluctuates.
The XAML is just
xmlns:chttk="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
<chttk:Chart Name="DataChart1" Margin="0" Title="{Binding AxisXTitle}" DataContext="{Binding GraphData1}" Style="{StaticResource ChartStyle1}" BorderBrush="Transparent"/>
The code behind is
Dim dataChart As System.Windows.Controls.DataVisualization.Charting.Chart = DataChart1
Dim s As New System.Windows.Controls.DataVisualization.Charting.BarSeries
s.Title = "My Chart"
s.DependentValuePath = "Value"
s.IndependentValuePath = "Key"
s.DataContext = New Binding("[0]")
s.ItemsSource = CType(itm, IEnumerable)
dataChart.Series.Add(s)
The DataContext is a DataSeries.Collection with the data in KeyValuePairs. The values are all Double values.
The graph is displaying, but the values on the axis are un-formatted, e.g. 120000. I have found a few examples pointing at different Axis types and LabelFormat and LabelStyle.Format but I can't find anything that works in the WPF environment in the code behind.
Thanks for the input - I finally managed to get it working with the following code:
Dim n As Integer
dataChart.Series.Clear()
Dim x As New System.Windows.Controls.DataVisualization.Charting.LinearAxis
x.Orientation = DataVisualization.Charting.AxisOrientation.X
Dim y As New System.Windows.Controls.DataVisualization.Charting.CategoryAxis
y.Orientation = DataVisualization.Charting.AxisOrientation.Y
Dim stl As New Style(GetType(System.Windows.Controls.DataVisualization.Charting.AxisLabel))
stl.Setters.Add(New Setter(System.Windows.Controls.DataVisualization.Charting.AxisLabel.StringFormatProperty, "{0:#,0}"))
x.AxisLabelStyle = stl
For Each itm As Object In o
Dim srs As New System.Windows.Controls.DataVisualization.Charting.BarSeries
pi = itm.GetType.GetProperty("SeriesTitle")
srs.Title = pi.GetValue(itm, Nothing)
srs.DependentValuePath = "Value"
srs.IndependentValuePath = "Key"
srs.DataContext = New Binding("[" & n & "]")
srs.ItemsSource = CType(itm, IEnumerable)
srs.DependentRangeAxis = x
srs.IndependentAxis = y
dataChart.Series.Add(srs)
n += 1
Next
Some of the gotchas that tripped me up were adding the axis. You need to add the right type of axis to the DependentRangeAxis or IndependentAxis. With a BarSeries the DependentRangeAxis is the X axis and can be a LinearAxis and the IndependentAxis is the Y axis and for me was a CategoryAxis. Also if you have multiple series then you need to apply the same axis to all series.
Hope this helps someone else.
I have this code that adds dotted lines under text in text box:
// Create an underline text decoration. Default is underline.
TextDecoration myUnderline = new TextDecoration();
// Create a linear gradient pen for the text decoration.
Pen myPen = new Pen();
myPen.Brush = new LinearGradientBrush(Colors.White, Colors.White, new Point(0, 0.5), new Point(1, 0.5));
myPen.Brush.Opacity = 0.5;
myPen.Thickness = 1.0;
myPen.DashStyle = DashStyles.Dash;
myUnderline.Pen = myPen;
myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;
// Set the underline decoration to a TextDecorationCollection and add it to the text block.
TextDecorationCollection myCollection = new TextDecorationCollection();
myCollection.Add(myUnderline);
PasswordSendMessage.TextDecorations = myCollection;
My problem is I need only the last 6 characters in the text to be formatted!
Any idea how can I achieve that?
Instead of setting the property on the entire TextBlock, create a TextRange for the last six characters and apply the formatting to that:
var end = PasswordSendMessage.ContentEnd;
var start = end.GetPositionAtOffset(-6) ?? PasswordSendMessage.ContentStart;
var range = new TextRange(start, end);
range.ApplyPropertyValue(Inline.TextDecorationsProperty, myCollection);
If PasswordSendMessage is a TextBox rather than a TextBlock, then you cannot use rich text like this. You can use a RichTextBox, in which case this technique will work but you will need to use PasswordSendMessage.Document.ContentEnd and PasswordSendMessage.Document.ContentStart instead of PasswordSendMessage.ContentEnd and PasswordSendMessage.ContentStart.
You could databind your text to the Inlines property of TextBox and make a converter to build the run collection with a seperate Run for the last 6 characters applying your decorations
everyone. There's probably a simple solution to this but I can't seem to find one. I'm playing around with the WebBrowser control in WPF that ships with Visual Studio 2010 and am trying to save an image that might appear on a webpage to disk programmatically.
Many thanks in advance!
Luck
Add System.Drawing as reference and perform the following oprations in the method that should capture the image:
Rect bounds = VisualTreeHelper.GetDescendantBounds(browser1);
System.Windows.Point p0 = browser1.PointToScreen(bounds.TopLeft);
System.Drawing.Point p1 = new System.Drawing.Point((int)p0.X, (int)p0.Y);
Bitmap image = new Bitmap((int)bounds.Width, (int)bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);
imgGraphics.CopyFromScreen(p1.X, p1.Y,
0, 0,
new System.Drawing.Size((int)bounds.Width,
(int)bounds.Height));
image.Save("C:\\a.bmp", ImageFormat.Bmp);
Here are the adaptions to solution of #luvieere:
WebBrowser browser1;
browser1 = this.Browser;
// I used the GetContentBounds()
Rect bounds = VisualTreeHelper.GetContentBounds(browser1);
// and the point to screen command for the top-left and the bottom-right corner
System.Windows.Point pTL = browser1.PointToScreen(bounds.TopLeft);
System.Windows.Point pBR = browser1.PointToScreen(bounds.BottomRight);
System.Drawing.Bitmap image = new
// The size is then calculated as difference of the two corners
System.Drawing.Bitmap(
System.Convert.ToInt32(pBR.X - pTL.X),
System.Convert.ToInt32(pBR.Y - pTL.Y));
System.Drawing.Graphics imgGraphics = System.Drawing.Graphics.FromImage(image);
imgGraphics.CopyFromScreen(pTL.X, pTL.Y, 0, 0, new System.Drawing.Size(image.Width, image.Height));
fileName = System.IO.Path.GetFileNameWithoutExtension(fileName) + ".bmp";
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
For those, who prefer VB-dialect
Dim browser1 As WebBrowser
browser1 = Me.Browser
Dim bounds As Rect = VisualTreeHelper.GetContentBounds(browser1)
Dim pTL As System.Windows.Point = browser1.PointToScreen(bounds.TopLeft)
Dim pBR As System.Windows.Point = browser1.PointToScreen(bounds.BottomRight)
Dim image As System.Drawing.Bitmap = New System.Drawing.Bitmap(CInt(pBR.X - pTL.X), CInt(pBR.Y - pTL.Y))
Dim imgGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(image)
imgGraphics.CopyFromScreen(pTL.X, pTL.Y, 0, 0, New System.Drawing.Size(image.Width, image.Height))
fileName = IO.Path.GetFileNameWithoutExtension(fileName) & ".bmp"
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp)
I have a Canvas and a custom control called BasicShape
After I add two BasicShape controls on the Canvas, I want programatically to connect them with a Line and I want to do this using the Binding class.
I want to connect the Bottom side of first shape with the Top side of the second one.
Initially i tried to connect only the X1 property of the Line with the Canvas.Left attached property of the fisrt BasicShape but this doesn't work. Line X1 property is not updated when I change the Canvas.SetLeft(basicShape1) value
BasicShape bs1 = canvas.Children[0] as BasicShape;
BasicShape bs2 = canvas.Children[1] as BasicShape;
Line line = new Line();
line.StrokeThickness = 1;
line.Stroke = new SolidColorBrush(Colors.Red);
line.X1 = 100;
line.Y1 = 100;
line.X2 = 200;
line.Y2 = 200;
canvas.Children.Add(line);
Binding b = new Binding("AnyName");
b.Source = bs1;
b.Path = new PropertyPath(Canvas.LeftProperty);
line.SetBinding(Line.X1Property, b);
I'm trying to create a simple UML diagram like this one
alt text http://www.invariant-corp.com/omechron/images/uml_diagram.gif
I just did it other way, without binding
This will be a permanent link
http://calciusorin.com/SilverlightDiagrams/
I decided to manually update all lines on shape Location or Size changed
private void basicShape_BasicShapeLocationSizeChangedEvent(BasicShape sender)
{
foreach (CustomLine customLine in lines)
{
if (customLine.StartFromShape(sender))
{
Point point = sender.GetLinePoint(customLine.GetStartSide());
customLine.SetStartPoint(point);
}
if (customLine.EndInShape(sender))
{
Point point = sender.GetLinePoint(customLine.GetEndSide());
customLine.SetEndPoint(point);
}
}
}
I am sure that the Binding solution is more elegant. Anyone interested in my solution, with SL Controls that can be resized, connected with lines, just contact me.