Programmatically creating a toolbar in WPF - wpf

I'm trying to create a simple toolbar in WPF, but the toolbar shows up with no corresponding buttons on it, just a very thin blank white strip. Any idea what I'm doing wrong, or what the recommended procedure is? Relevant code fragments so far:
var tb = new ToolBar();
var b = new Button();
b.Command = comback;
Image myImage = new Image();
myImage.Source = new BitmapImage(new Uri("back.png", UriKind.Relative));
b.Content = myImage;
tb.Items.Add(b);
var p = new DockPanel();
//DockPanel.SetDock(mainmenu, Dock.Top);
DockPanel.SetDock(tb, Dock.Top);
DockPanel.SetDock(sb, Dock.Bottom);
//p.Children.Add(mainmenu);
p.Children.Add(tb);
p.Children.Add(sb);
Content = p;

Without a third child-element for the Dockpanel p, the 'sb' element will fill everything except for the area of tb. The ToolBar will autoSize (its Height) according to its contents.
I suggest adding a simple text button first, to check the overall layout:
var b2 = new Button();
b2.Content = "B2";
tb.Items.Add(b2);
And then investigate what is wrong with the "back.png" image.

Related

VisualBrush does "lazy evaluation"?

I create FixedDocument in more iterations (one page per iteration) like this:
PrintDialog pr = new PrintDialog();
FixedDocument doc = new FixedDocument();
foreach(var i in a)
{
// some changes of MaingGrid here
...
//
VisualBrush vb = new VisualBrush(this.MainGrid);
FixedPage page = new FixedPage();
page.Width = doc.DocumentPaginator.PageSize.Width;
page.Height = doc.DocumentPaginator.PageSize.Height;
Rectangle rec = new Rectangle();
rec.Width = this.MainGrid.ActualWidth;
rec.Height = this.MainGrid.ActualHeight;
rec.Fill = vb;
page.Children.Add(rec);
PageContent content = new PageContent();
((IAddChild)content).AddChild(page);
doc.Pages.Add(content);
}
pr.PrintDocument(doc.DocumentPaginator, "test");
In each iteration I change the MainGrid a little. So each page should contain the actual state of MainGrid. But the printed document contains pages with same content of last iteration (in other words - the last state is on all pages in document). Is there any "lazy evaluation" of VisualBrush or something?
Call .Freeze() on the VisualBrush in each iteration. Otherwise, it will always be a live view of whatever visual you pointed it at.
EDIT: Freeze doesn't work but you can render the brush into a static bitmap. See http://blog.avanadeadvisor.com/blogs/markti/archive/2008/04/14/10888.aspx

dynamically added control not rendering, silverlight

Hello i have a canvas with the name Layout. I want to add some childs to it in code behind. Here is the code.
private void AddItem(int TruePosition, int CurrentPosition, string ImageFileName)
{
Image img = new Image();
img.Source = new BitmapImage(new Uri(#"/Images/" + ImageFileName, UriKind.Relative));
img.Width = 100.0;
img.Height = 400.0;
img.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
img.Stretch = Stretch.None;
Border b = new Border();
b.SetValue(Canvas.TopProperty, 200.0);
b.SetValue(Canvas.LeftProperty, (double)CurrentPosition);
b.SetValue(Canvas.ZIndexProperty, 1);
b.Background = new SolidColorBrush(Colors.Blue);
b.MouseMove += new MouseEventHandler(Border_MouseMove);
b.MouseLeftButtonDown += new MouseButtonEventHandler(Border_MouseLeftButtonDown);
b.MouseLeftButtonUp += new MouseButtonEventHandler(Border_MouseLeftButtonUp);
b.Child = img;
Layout.Children.Add(b);
UpdateLayout();
}
The image Uri is probably good because if i try to add only the border(without the image) it's still not rendering. Also i call this AddItem function from a button click event handler so initializations should not be a problem. Probably i am missing a very basic stuff here. Any ideas are welcome, thank you for your help.
Update: Ok i had something wrong with the browser cache probably, the border is added, but the image in the border isn't showing up. In the Visual Studio project i have an Images dir with the image files. Am i setting the path to them wrong?
So, now i am allowed to answer it. Well it was a newbie mistake.
The first / before Images is not needed. new Uri(#"Images/" + ImageFileName, UriKind.Relative) is correct.

Silverlight Image Cropping

Does anybody knows how to image cropping in silverlight without any library.
I have Child window and inside the child window I havev a image and this image center one rectange is there so I can panning the image to the around the rectange and selecet the perticular part of the image and this selected part I want to crop.
Also I am using WriteableBitmap and try to Crop, this will not work if correct me if I am wrong.
sheetRectangle.Children is the Image.
foreach (ucPicRect item in sheetRectangle.Children)
{
WriteableBitmap obj = new WriteableBitmap(item.imgCell.Source as BitmapSource);
obj.Crop(0,0,400,400);
obj.Invalidate();
item.imgCell.Effect = dlgcwEditPhoto.imgEdit.Effect;
item.imgCell.Source = obj;// dlgcwEditPhoto.imgEdit.Source;
}
Thanks...!!!
you can use this utility function to crop your image
public static WriteableBitmap cropImage(Image image, double[] coordonnee)
{
Image cloneImage = new Image();
cloneImage.Source = image.Source;
RectangleGeometry myRec = new RectangleGeometry();
myRec.Rect = new Rect(coordonnee[0], coordonnee[1], coordonnee[2], coordonnee[3]);
cloneImage.Clip = myRec;
TranslateTransform t = new TranslateTransform();
t.X = -coordonnee[0];
t.Y = -coordonnee[1];
WriteableBitmap wb = new WriteableBitmap(cloneImage, t);
wb.Invalidate();
return wb;
}
good luck !!

WPF/Path - geometry consisting of multiple lines with different line width?

I have the need for drawing in the same drawing with lines of different color, thickness etc.
I can create two instances of PathGeometry, but I can't set color on them.
I can create two instances of Path, but can't get them displayed in my control.
What am I doing wrong?
Thanks for any comments!
Anders, Denmark.
Code below only displays "collection" in my control, but I thought it could be a starting point for answers...
var pathFigure1 = new PathFigure(new Point(0, 0),
new List<PathSegment> {new LineSegment(new Point(10, 10), true)}, false);
var pathFigure2 = new PathFigure(new Point(20, 20),
new List<PathSegment> {new LineSegment(new Point(30, 30), true)}, false);
var g1 = new PathGeometry(new List<PathFigure> {pathFigure1});
var g2 = new PathGeometry(new List<PathFigure> {pathFigure2});
var p1 = new System.Windows.Shapes.Path
{
Data = g1,
Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0))
};
var p2 = new System.Windows.Shapes.Path
{
Data = g2,
Stroke = new SolidColorBrush(Color.FromRgb(170, 87, 170))
};
var content = new Canvas();
content.Children.Add(p1);
content.Children.Add(p2);
Content = content;
You have started on the right approach, a geometry defines a 'shape', so don;t worry that you cannot set its colour. A Path turns the geometry into a visual representation on the screen, so here you can set the color and stroke thickness.
Your problem is at the very last step, you are setting the content property of your control. Typically this property is used to associate some data object with a control, you then supply a template which is its visual representation.
What you need to do is add your paths as children of a panel.
For example, add a Canvas, or a Grid to your control. Then add your two paths to the Grid / Canvas via their Children collection property.

WPF - Text box that grows vertically to accommodate all text

The problem: I am not getting a textbox setting that will have a horizontally wordwrap and vertically auto grow functionality. I wish to do that by writing a code. I have written following code that creates a text box at mouse dblclick with wordwrap:
TextBox text2 = new TextBox();
text2.Width = 500;
text2.Visibility = Visibility.Visible;
text2.Focus();
text2.Height = 30;
text2.HorizontalAlignment = HorizontalAlignment.Left;
text2.VerticalAlignment = VerticalAlignment.Top;
Point p = e.GetPosition(LayoutRoot);
text2.Margin = new Thickness(p.X, p.Y, 0, 0);
LayoutRoot.Children.Add(text2);
But, textbox does not grow vertically.
Can somebody suggest me a code in C# to do exactly what I desire?
try using this
Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition());
TextBox textBox = new TextBox() { Width = 100, TextWrapping = TextWrapping.Wrap };
textBox.SetValue(Grid.RowProperty, 0);
grid.Children.Add(textBox);
window.Content = grid;
where window is the Name assigned to Window(root).
One way to accomplish the growth you're looking for is to use a string measuring mechanism which you would run any time the text in your text box changes. Simply measure and resize your text box accordingly with any change to the contents.
Have you tried this?
text2.Height = double.NaN; // or don't set the property, but some custom styles might give a default value ..
text2.TextWrapping = TextWrapping.Wrap;
text2.MinHeight = 30; // or not if you want the styles default
instead of
text2.Height = 30;
not setting it or using double.NaN is the same as using 'Auto' in xaml.

Resources