GridSplitter Not present - wpf

I use this in code behind to test how to add a grid splitter programmatically. (I know, don't use code behind - But this is one of those rare cases I need to. (I think))
public partial class ContainerView : Window, IContainerView
{
[ImportingConstructor]
public ContainerView()
{
InitializeComponent();
SetUp();
}
public void SetUp()
{
_grid = new Grid();
//Single column/single row
_grid.ColumnDefinitions.Add(new ColumnDefinition());
_grid.ColumnDefinitions.Add(new ColumnDefinition());
_grid.ColumnDefinitions.Add(new ColumnDefinition());
_grid.RowDefinitions.Add(new RowDefinition());
_grid.RowDefinitions.Add(new RowDefinition());
var button1 = new Button();
button1.Content = "Btn 1";
button1.Margin = new Thickness(5);
Grid.SetRow(button1, 0);
Grid.SetColumn(button1, 0);
var button2 = new Button();
button2.Content = "Btn 2";
button2.Margin = new Thickness(5);
Grid.SetRow(button2, 1);
Grid.SetColumn(button2, 2);
_grid.Children.Add(button1);
_grid.Children.Add(button2);
var splitterV = new GridSplitter();
Grid.SetRowSpan(splitterV, _grid.RowDefinitions.Count);
splitterV.VerticalAlignment = VerticalAlignment.Stretch;
splitterV.HorizontalAlignment = HorizontalAlignment.Right;
splitterV.ShowsPreview = true;
splitterV.Background = Brushes.Black;
Width = 5;
_grid.Children.Add(splitterV);
Grid.SetColumn(splitterV, 1);
Content = _grid;
}
I can see the two buttons, but the middle column is empty. The GridSplitter is not shown. What am I doing wrong?

You are absolutely correct but you are setting Window's width instead of GridSpitter's width & also you have to give it's ResizeBehaviour.
Instead of this :
Width = 5;
Set GridSpitter's Width & It's ResizeBehavior as :
splitterV.ResizeBehavior = GridResizeBehavior.PreviousAndNext;
splitterV.Width = 5;

Related

Resizing of an AutoScroll Panel affects scrolled position

When I resize the following form with the right resize handle, the contained TableLayoutPanel gets decorated with scroll bars (as intended, panel1.AutoScroll = true) for smaller form sizes, but the TableLayoutPanel also gets displaced from its original position. See images below: after resizing the form with right resize handle only, the second one has its scroll bars not leftmost and the left border of the content is cut off.
It seems somehow that this behavior is tied to the existence of the nested RadioButtons because if I remove them (or replace them by another TextBox for example), the "normal" behavior is restored (TableLayoutPanel stays in place during resize).
What properties do I have to set in order to keep the content always stationary relative to the (top)left borders?
BTW: When I replace the panel1 by a TabControl + one TabPage, the "normal" behavior is also restored.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
/// <summary>
/// Description of Form3.
/// </summary>
public partial class Form3 : Form
{
const int textBoxNameWidth = 500;
TableLayoutPanel testControl1;
Panel panel1;
TextBox textBoxName;
RadioButton radioButtonNo;
RadioButton radioButtonYes;
TableLayoutPanel tableLayoutPanelDecision;
public Form3()
{
testControl1 = new TableLayoutPanel();
panel1 = new Panel();
textBoxName = new TextBox();
radioButtonNo = new RadioButton();
radioButtonYes = new RadioButton();
tableLayoutPanelDecision = new TableLayoutPanel();
testControl1.AutoSize = true;
testControl1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
testControl1.Location = new Point(0, 0);
testControl1.Dock = DockStyle.None;
testControl1.ColumnCount = 2;
testControl1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
testControl1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
testControl1.RowCount = 2;
testControl1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
testControl1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
testControl1.Controls.Add(textBoxName, 1, 0);
testControl1.Controls.Add(tableLayoutPanelDecision, 1, 1);
textBoxName.Text = "New Boolean";
textBoxName.TextAlign = HorizontalAlignment.Center;
textBoxName.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
textBoxName.TabStop = false;
textBoxName.Width = textBoxNameWidth;
tableLayoutPanelDecision.AutoSize = true;
tableLayoutPanelDecision.ColumnCount = 2;
tableLayoutPanelDecision.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
tableLayoutPanelDecision.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
tableLayoutPanelDecision.RowCount = 1;
tableLayoutPanelDecision.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanelDecision.Controls.Add(radioButtonYes, 0, 0);
tableLayoutPanelDecision.Controls.Add(radioButtonNo, 1, 0);
tableLayoutPanelDecision.Dock = DockStyle.Fill;
radioButtonNo.Checked = true;
radioButtonNo.AutoSize = true;
radioButtonNo.TabIndex = 1;
radioButtonNo.TabStop = true;
radioButtonNo.Text = "False";
radioButtonNo.UseVisualStyleBackColor = true;
radioButtonNo.Anchor = AnchorStyles.None;
radioButtonYes.AutoSize = true;
radioButtonYes.TabIndex = 0;
radioButtonYes.Text = "True";
radioButtonYes.UseVisualStyleBackColor = true;
radioButtonYes.Anchor = AnchorStyles.None;
panel1.AutoScroll = true;
panel1.Controls.Add(testControl1);
panel1.Dock = System.Windows.Forms.DockStyle.Fill;
panel1.Location = new System.Drawing.Point(0, 0);
panel1.Name = "panel1";
panel1.Size = new System.Drawing.Size(560, 219);
panel1.TabIndex = 1;
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(560, 219);
Controls.Add(panel1);
Name = "Form3";
Text = "Form3";
}
}
}
The panel is trying to keep focusable controls within view for the user. To change that, you would have to use your own panel:
public class PanelEx : Panel {
protected override Point ScrollToControl(Control activeControl) {
return this.DisplayRectangle.Location;
}
}

WPF - pass data from page A to page B

So I want to pass data when I clicked an Canvas. So I have this code;
Canvas event_canvas = new Canvas();
event_canvas.Background = new SolidColorBrush(Color.FromRgb(66, 70, 77));
event_canvas.Width = 250;
event_canvas.Height = 60;
event_canvas.Margin = new Thickness(40, 0, 0, 0);
event_canvas.HorizontalAlignment = HorizontalAlignment.Left;
event_canvas.VerticalAlignment = VerticalAlignment.Top;
event_canvas.Cursor = Cursors.Hand;
#endregion
#region Grid (event_grid)
Grid event_grid = new Grid();
event_grid.Width = 250;
event_grid.Height = 60;
#endregion
#region TextBlock (event_text)
TextBlock event_text = new TextBlock();
event_text.VerticalAlignment = VerticalAlignment.Center;
event_text.HorizontalAlignment = HorizontalAlignment.Center;
event_text.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255));
event_text.Text = e.name;
#endregion
event_grid.Children.Add(event_text); // Add the textblock to the grid
event_canvas.Children.Add(event_grid); // Add the grid to the canvas
grid_events.Children.Add(event_canvas); // Add the canvas to the main grid.
// Click event registration
event_canvas.MouseLeftButtonDown += Event_canvas_MouseLeftButtonDown;
And then in the trigger;
private void Event_canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Page pg = new EventDetailPage();
// Replaces all the content!!!!!
this.Content = pg;
//throw new NotImplementedException();
}
I tried to add this;
var param = ((TextBlock)sender).Text;
Page pg = new EventDetailPage(param);
But that code doesn't work, it throws an error that I can't get a value.
How can I fix this issue?
Cast the sender argument to Canvas and then access the Grid through the Canvas' Children collecton and the TextBlock through the Grid's Children collecton:
private void Event_canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Canvas canvas = (Canvas)sender;
Grid event_grid = canvas.Children[0] as Grid;
TextBlock event_text = event_grid.Children[0] as TextBlock;
string text = event_text.Text;
...
}

WPF UserControl background Transparency doesn't work

I try to pop up a usercontrol using own class ModalDialogManager : Control but the background doesn't seem transparent as shown on http://prntscr.com/6cgyc2
in my ModalDialogManager class, I did specify the windows to allow transparency:
Window w = new Window();
m_window = w;
w.Closing += w_Closing;
w.Owner = GetParentWindow(this);
w.DataContext = this.DataContext;
w.SetBinding(Window.ContentProperty, "");
w.Title = Title;
w.Icon = Icon;
w.Height = DialogHeight;
w.Width = DialogWidth;
w.ResizeMode = DialogResizeMode;
// SHOULD IT WORK?!
w.AllowsTransparency = true;
double t = GetParentWindow(this).Left;
if (IsBorderless)
{
w.WindowStyle = WindowStyle.None;
w.ShowInTaskbar = false;
if (IsStartUpLocationCenter)
{
w.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
else
{
w.Left = LeftPosition;
w.Top = RightPosition;
w.WindowStartupLocation = WindowStartupLocation.Manual;
}
}
else
{
w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
}
if (IsModal)
w.ShowDialog();
else
w.Show();
but the UserControl design shows ok: http://prntscr.com/6cgyz8
Thus, I tried to use a window to attach the usercontrol and do window.showDialog() with a usercontrol as below:
Window w = new Window();
SolidColorBrush b = new SolidColorBrush();
b.Color = .Colors.Transparent;
w.Background = b;
Grid g = new Grid();
g.Children.Add(new ucSelectCloth());
w.Content = g;
g.Background = b;
w.Height = 300;
w.Width = 600;
w.ShowDialog();
As you can see that http://prntscr.com/6cgzdk the window doesn't look Transparent too.
Any thought?
window remains opaque until you set Window.AllowsTransparency property to true. Once you set Window.AllowsTransparency to true you have to set WindowStyle to None.
add following lines to your code,
w.WindowStyle = System.Windows.WindowStyle.None;
w.AllowsTransparency = true;

Print Grid which generated dynamically in wpf

i want to print a grid which is generated dynamically.
Means, in the click event of the Print Button, i m generating a grid and then i want to print that grid.
here is my code,
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
PrintDialog Objprint = new System.Windows.Controls.PrintDialog();
if (Objprint.ShowDialog() == true)
{
System.Printing.PrintCapabilities capabilities = Objprint.PrintQueue.GetPrintCapabilities(Objprint.PrintTicket);
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight / this.ActualHeight);
#region "Make a grid For Printing"
Grid objgrid = new Grid();
objgrid.Name = "GridForPrinting";
objgrid.Width = 1000;
objgrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
objgrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objgrid.RowDefinitions.Add(new RowDefinition());
TextBlock objtext = new TextBlock();
objtext.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
objtext.VerticalAlignment = System.Windows.VerticalAlignment.Center;
objtext.Text = "SUPERIOR COURT OF CALIF COUNTY OF SAN BERNARDINO";
Grid.SetRow(objtext, 0);
objgrid.Children.Add(objtext);
#endregion
Objprint.PrintVisual(objgrid, "Case Summary");
}
}
this code give me blank page to print.
how can i do that?
here i get answer,
from the below code i get what i want to do...
void PrintOnClick(object sender, RoutedEventArgs args)
{
PrintDialog dlg = new PrintDialog();
if ((bool)dlg.ShowDialog().GetValueOrDefault())
{
// Create Grid panel.
Grid grid = new Grid();
// Define 5 auto-sized rows and columns.
for (int i = 0; i < 5; i++)
{
ColumnDefinition coldef = new ColumnDefinition();
coldef.Width = GridLength.Auto;
grid.ColumnDefinitions.Add(coldef);
RowDefinition rowdef = new RowDefinition();
rowdef.Height = GridLength.Auto;
grid.RowDefinitions.Add(rowdef);
}
// Give the Grid a gradient brush.
grid.Background =
new LinearGradientBrush(Colors.Black, Colors.White,
new Point(0, 0), new Point(1, 1));
// Every program needs some randomness.
Random rand = new Random();
// Fill the Grid with 25 buttons.
for (int i = 0; i < 25; i++)
{
Button btn = new Button();
btn.FontSize = 12 + rand.Next(8);
btn.Content = "Button No. " + (i + 1);
btn.HorizontalAlignment = HorizontalAlignment.Center;
btn.VerticalAlignment = VerticalAlignment.Center;
btn.Margin = new Thickness(6);
grid.Children.Add(btn);
Grid.SetRow(btn, i % 5);
Grid.SetColumn(btn, i / 5);
}
// Size the Grid.
grid.Measure(new Size(Double.PositiveInfinity,
Double.PositiveInfinity));
Size sizeGrid = grid.DesiredSize;
// Determine point for centering Grid on page.
Point ptGrid =
new Point((dlg.PrintableAreaWidth - sizeGrid.Width) / 2,
(dlg.PrintableAreaHeight - sizeGrid.Height) / 2);
// Layout pass.
grid.Arrange(new Rect(ptGrid, sizeGrid));
// Now print it.
dlg.PrintVisual(grid, Title);
}
}
The PrintVisual print a Visual object. That means, by using the PrintVisual method, we can print any control, container, Window or user control that is in the visualtree.You cannot print a control that is not in the visualtree

Problem adding Viewport2DVisual3D from Code

I'm trying to add a Viewport2DVisual3D to a Viewport3D in code, but the visual isn't showing up. Any help understanding why not would be appreciated. The following is the code for the main window.
Is it sufficient to just add the Viewport2DVisual3D to the children of the Viewport3D in order for it to be rendered?
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(temp);
}
public void temp(object sender, RoutedEventArgs e)
{
Viewport2DVisual3D test = new Viewport2DVisual3D();
MeshGeometry3D testGeometry = new MeshGeometry3D();
Vector3D CameraLookDirection = Main_Target_CameraOR20.LookDirection;
// Calculate the Positions based on the Camera
Point3DCollection myPoint3DCollection = new Point3DCollection();
myPoint3DCollection.Add(new Point3D(-1, 1, 0));
myPoint3DCollection.Add(new Point3D(-1, -1, 0));
myPoint3DCollection.Add(new Point3D(1, -1, 0));
myPoint3DCollection.Add(new Point3D(1, 1, 0));
testGeometry.Positions = myPoint3DCollection;
PointCollection myPointCollection = new PointCollection();
myPointCollection.Add(new Point(0, 0));
myPointCollection.Add(new Point(0, 1));
myPointCollection.Add(new Point(1, 1));
myPointCollection.Add(new Point(1, 0));
testGeometry.TextureCoordinates = myPointCollection;
Int32Collection triangleIndicesCollection = new Int32Collection();
triangleIndicesCollection.Add(0);
triangleIndicesCollection.Add(1);
triangleIndicesCollection.Add(2);
triangleIndicesCollection.Add(2);
triangleIndicesCollection.Add(3);
triangleIndicesCollection.Add(0);
testGeometry.TriangleIndices = triangleIndicesCollection;
DiffuseMaterial myDiffuseMaterial = new DiffuseMaterial(Brushes.White);
Viewport2DVisual3D.SetIsVisualHostMaterial(myDiffuseMaterial, true);
Transform3DGroup myTransform3DGroup = new Transform3DGroup();
ScaleTransform3D myScaleTransform3D = new ScaleTransform3D();
myScaleTransform3D.ScaleX = 2;
myScaleTransform3D.ScaleY = 2;
myScaleTransform3D.ScaleZ = 2;
TranslateTransform3D myTranslateTransform3D = new TranslateTransform3D();
myTranslateTransform3D.OffsetX = -27;
myTranslateTransform3D.OffsetY = 13;
myTranslateTransform3D.OffsetZ = 6;
RotateTransform3D rotateTransform = new RotateTransform3D()
{
Rotation = new AxisAngleRotation3D
{
Angle = -50,
Axis = new Vector3D(0, 1, 0)
}
};
myTransform3DGroup.Children.Add(myTranslateTransform3D);
myTransform3DGroup.Children.Add(myScaleTransform3D);
myTransform3DGroup.Children.Add(rotateTransform);
test.Transform = myTransform3DGroup;
Button myButton = new Button();
myButton.Content = "Test Button";
test.Material = myDiffuseMaterial;
test.Geometry = testGeometry;
test.Visual = myButton;
ZAM3DViewport3D.Children.Add(test);
}
}
It turns out that the problem was the Offset value. So, it is sufficient to add the child to the Viewport3D to have it render. Cheers

Resources