WPF Table Column Sizes - wpf

I'm rendering a Table in a WPF FlowDocument using code-behind. But, I've been unable to find an example that shows how to make the table use only the space needed based on content. Instead the table takes up all available width, which I don't want, nor do I want to have to specify a exact pixel sizes.
I'm clearly missing something simple, anyone see it?
var fd = new FlowDocument();
Table t = new Table();
t.BorderBrush = Brushes.Black;
t.BorderThickness = new Thickness(2);
// I thought this would do what I wanted...
t.Columns.Add(new TableColumn() { Width = GridLength.Auto });
t.Columns.Add(new TableCOlumn() { Width = GridLength.Auto });
TableRowGroup trg = new TableRowGroup();
TableRow currentRow = new TableRow();
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("ABC"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("XYZ"))));
trg.Rows.Add(currentRow);
currentRow = new TableRow();
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("123"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("789"))));
trg.Rows.Add(currentRow);
t.RowGroups.Add(trg);
fd.Blocks.Add(t);

I do not think this is possible... the only hacky workaround is to use the BlockUIContainer and a real grid!
var fd = new FlowDocument();
Grid g = new Grid();
g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
g.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
g.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var t1 = new TextBlock() { Text = "ABC", Margin = new Thickness(5,3,5,3) };
t1.SetValue(Grid.ColumnProperty, 0);
t1.SetValue(Grid.RowProperty, 0);
g.Children.Add(t1);
var t2 = new TextBlock() { Text = "XYZ", Margin = new Thickness(5, 3, 5, 3) };
t2.SetValue(Grid.ColumnProperty, 1);
t2.SetValue(Grid.RowProperty, 0);
g.Children.Add(t2);
var t3 = new TextBlock() { Text = "123", Margin = new Thickness(5, 3, 5, 3) };
t3.SetValue(Grid.ColumnProperty, 0);
t3.SetValue(Grid.RowProperty, 1);
g.Children.Add(t3);
var t4 = new TextBlock() { Text = "789", Margin = new Thickness(5, 3, 5, 3) };
t4.SetValue(Grid.ColumnProperty, 1);
t4.SetValue(Grid.RowProperty, 1);
g.Children.Add(t4);
fd.Blocks.Add(new BlockUIContainer(g));

try TableCell.ColoumnSpan and TableCell.RowSpan

I know the question was asked 9 years ago, but I found out an alternate way to do this without using a BlockUIContainer which quite frankly is a pain when serializing the FlowDocument or when the user is just editing the document in a RichTextBox.
Add a PreviewMouseMove and PreviewMouseDown handler to every single table cells. In PreviewMouseMove change the cursor to SizeWE whenever adjacent to the cell border. In PreviewMouseDown capture the mouse using the RichTextBox as the source.
Add a PreviewMouseMove and PreviewMouseUp handler to the RichTextBox. In PreviewMouseMove resize the table column based on the calculated horizontal delta movement. In PreviewMouseUp release the mouse.
The tricky part is to figure where the cell borders are because there's no way out of the box to just get the cell position or width. So you have to approximate where they are by doing the sum of the PagePadding, Table Padding and column widths.

Related

GridView Docking Issue

I have a gridview and textbox control in my form. Textbox is on left top and under gridview anchored left, right and bottom. I want it saves the distance with textbox above it. This is my code below and it doesn't save the distance between textbox. when i make the window full screen there are so much distance between them as image.
dataGridView1.Anchor = (AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom);
this is what you need
private void Form2_Load(object sender, EventArgs e)
{
//just setting panel color to red. so you can see the panel.
var panel = new Panel() { BackColor= Color.Red};
this.Controls.Add(panel);
panel.Dock = DockStyle.Top;
var textBox = new TextBox() { Top = 20, Left = 20, Height=10, Width=330};
panel.Controls.Add(textBox);
var dataGridView = new DataGridView() ;
dataGridView.Columns.Add("Name","Text");
this.Controls.Add(dataGridView);
dataGridView.AutoGenerateColumns = true;
dataGridView.DataSource = Enumerable.Range(0, 10).ToList();
dataGridView.BringToFront();
dataGridView.Dock = DockStyle.Fill;
}

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 creating Silverlight Grid columns programmatically on WP7

I'm trying to create a user control which will allow me to pass in a list of column definitions and which will create a grid for me.
So far nothing is displaying. Here is the code:
List<GridColumn> colList = new List<GridColumn>();
GridColumn col1 = new GridColumn(200, "AAA");
colList.Add(col1);
GridColumn col2 = new GridColumn(200, "BBB");
colList.Add(col2);
BuildColumns(MainGrid, colList)
private void BuildColumns(Grid mainGrid, List<GridColumn> gridColumnList)
{
// create grid columns
foreach (GridColumn gridColumn in gridColumnList)
{
GridLength len = new GridLength(gridColumn.ColumnWidth);
ColumnDefinition col = new ColumnDefinition {Width = len};
mainGrid.ColumnDefinitions.Add(col);
}
// add 2 rows
GridLength height = new GridLength(100);
RowDefinition rowDef1 = new RowDefinition {Height = height};
mainGrid.RowDefinitions.Add(rowDef1);
RowDefinition rowDef2 = new RowDefinition {Height = height};
mainGrid.RowDefinitions.Add(rowDef2);
// add text blocks to cells
int colNum = -1;
foreach (GridColumn gridColumn in gridColumnList)
{
colNum++;
TextBlock textBlock = new TextBlock();
textBlock.Text = gridColumn.ColumnName;
Grid.SetRow(textBlock, 0);
Grid.SetColumn(textBlock, colNum);
}
}
I have tried increasing the row/column size and refreshing the grid.
This is the same as this earlier question but the answer did not fix my problem.
This is what it looks like on the phone
The problem is that you aren't actually adding the TextBlock elements into the visual tree. You need to add them to the Children collection on the mainGrid Grid element supplied to the BuildColumns method.
private void BuildColumns(Grid mainGrid, List gridColumnList)
{
// create grid columns
foreach (GridColumn gridColumn in gridColumnList)
{
GridLength len = new GridLength(gridColumn.ColumnWidth);
ColumnDefinition col = new ColumnDefinition { Width = len };
mainGrid.ColumnDefinitions.Add(col);
}
// add 2 rows
GridLength height = new GridLength(100);
RowDefinition rowDef1 = new RowDefinition {Height = height};
mainGrid.RowDefinitions.Add(rowDef1);
RowDefinition rowDef2 = new RowDefinition {Height = height};
mainGrid.RowDefinitions.Add(rowDef2);
// add text blocks to cells
int colNum = -1;
foreach (GridColumn gridColumn in gridColumnList)
{
colNum++;
TextBlock textBlock = new TextBlock();
textBlock.Text = gridColumn.ColumnName;
Grid.SetRow(textBlock, 0);
Grid.SetColumn(textBlock, colNum);
mainGrid.Children.Add(textBlock); // This line makes all the difference.
}
}

WPF image thumbnail with the offset

I have an image 800x600 and I would show a thumbnail 90x30 with some offset x=12 and y 12.
I have created a brush but I am struggling to apply an offset.
var source = new ImageBrush(groundSource);
source.Stretch = Stretch.None;
source.AlignmentX = AlignmentX.Left;
source.AlignmentY = AlignmentY.Top;
source.RelativeTransform = new TranslateTransform(0.5, 0);
var grid = new Grid();
grid.ClipToBounds = true;
grid.Background = source;
grid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
grid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
grid.Margin = new System.Windows.Thickness(12, 12, 0, 0);
grid.Width = SpriteSize.SpriteWidht + 33;
grid.Height = SpriteSize.SpriteHeight;
grid.SnapsToDevicePixels = true;
I would appreciate any suggestions.
There is a hacky solution for this problem:
Add Image as a child to the Grid.
Set Grid attribute as ClipToBounds=true
Set Image margin to control thumbnail offset.

Scaled ellipse over button, button not clickable

I'm scaling an ellipse in an animation with the following code:
ScaleTransform myScTransform = new ScaleTransform();
TransformGroup myTransGroup = new TransformGroup();
myTransGroup.Children.Add(myScTransform);
newPHRadio.RenderTransform = myTransGroup;
newPHRadio.RenderTransformOrigin = new Point(0.5, 0.5);
Storyboard story = new Storyboard();
DoubleAnimation xAnimation = new DoubleAnimation(1, ph.Bereik, new Duration(TimeSpan.FromSeconds(2)));
DoubleAnimation yAnimation = new DoubleAnimation(1, ph.Bereik, new Duration(TimeSpan.FromSeconds(2)));
DoubleAnimation doorzichtig = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(2)));
Storyboard.SetTarget(xAnimation, newPHRadio);
Storyboard.SetTarget(yAnimation, newPHRadio);
Storyboard.SetTarget(doorzichtig, newPHRadio);
DependencyProperty[] propertyChainX = new DependencyProperty[] {
Ellipse.RenderTransformProperty,
TransformGroup.ChildrenProperty,
ScaleTransform.ScaleXProperty
};
DependencyProperty[] propertyChainY = new DependencyProperty[] {
Ellipse.RenderTransformProperty,
TransformGroup.ChildrenProperty,
ScaleTransform.ScaleYProperty
};
string thePath = "(0).(1)[0].(2)";
Storyboard.SetTargetProperty(xAnimation, new PropertyPath(thePath, propertyChainX));
Storyboard.SetTargetProperty(yAnimation, new PropertyPath(thePath, propertyChainY));
Storyboard.SetTargetProperty(doorzichtig, new PropertyPath(Ellipse.OpacityProperty));
story.Children.Add(xAnimation);
story.Children.Add(yAnimation);
story.Children.Add(doorzichtig);
story.Duration = new Duration(TimeSpan.FromSeconds(60 / ph.Frequentie));
story.RepeatBehavior = RepeatBehavior.Forever;
story.Begin();
The ellipse is constructed with the following code:
Ellipse newPHRadio = new Ellipse();
newPHRadio.Width = 1;
newPHRadio.Height = 1;
newPHRadio.SetValue(Canvas.LeftProperty, ph.xPositie + 7);
newPHRadio.SetValue(Canvas.TopProperty, ph.yPositie + 7);
newPHRadio.SetValue(Canvas.ZIndexProperty, 3);
newPHRadio.Stroke = new SolidColorBrush(Colors.Black);
newPHRadio.StrokeThickness = 0.03;
Now the ellipse is scaled over an button which has a z-index of 1. With a static ellipse and no fill, the button is clickable. Now there is no fill as well but the button is not clickable. Can someone tell me how to fix this?
With the code you provided the button is clickable.
But if you set the Fill of the ellipse to anything but null, even to Brushes.Transparent, the click will not make it to the button anymore.
Try explicitly setting the Fill of the ellipse to null:
newPHRadio.Fill = null;

Resources