TextBlock Width measurement is differed for WinRT and WPF - c#

I need to return width of GridComboBoxColumn based on the maximum length of combotext mentioned below, which is loaded as its ItemsSource . The maximum combotext is "Frankfurt a.M.-Germany-Country" and the width is calculted in WinRT and WPF as follows:
//Calculate width for maximum display text ComboBox items
TextBlock TextBlock = new TextBlock()
{
FontFamily = FontFamily,
Margin = Margin,
FontSize = FontSize,
TextWrapping=TextWrapping.NoWrap
};
//Set the TextBlock display text to combo items text and initialize Height and Width for TextBlock
TextBlock.Text = DisplayText;
var parentBorder = new Border { Child = TextBlock };
TextBlock.MaxHeight = size.Height;
TextBlock.MaxWidth = double.MaxValue;
this.DataGrid.UpdateLayout();
//Measuer the width and Height of parentBorder
parentBorder.Measure(new Size(TextBlock.MaxWidth, TextBlock.MaxHeight));
parentBorder.Child = null;
return parentBorder.DesiredSize.Width;
In WPF, the width returned as 182.46 and in WinRT the width is returned as 248 . But in WinRT the column width fit with ComboBox, in WPF the ComboBox does not fit inside the column width. Please advise how the size is measured in both WPF and WinRT?

Related

ColorSpectrum Width and Height can't be Changed in UWP?

ColorSpectrum ColorSpectrum = new ColorSpectrum();
ColorSpectrum.Width = 400;
ColorSpectrum.Height = 180;
But the Width and height cant be changed..How to change it?
But the Width and height cant be changed..How to change it?
Unfortunately, the default shape of ColorSpectrum is square, because a user will has more control when they select a specific color using a square because more of the color gamut is shown. So ColorSpectrum will limit by the minimum value of Width Height . If you want change the size, please set Width and Height same value.
ColorSpectrum ColorSpectrum = new ColorSpectrum();
ColorSpectrum.Width = ColorSpectrum.Height = 400;

Find Height of Textblock / Text

i try to calculate the height of TextBlock.
I dont want to take the Textblock on some page. I just have to know the height of my Content.
I tried it this way, but it just returns 0
private static double CalculateContentHeight(string content, double width, double fontSize)
{
//Nur einen Textblock ggf. um Ressourcen zu sparen
TextBlock textblock = new TextBlock
{
FontSize = fontSize,
Text = content,
TextWrapping = TextWrapping.Wrap,
MaxWidth = width,
Height = Double.NaN
};
return textblock.ActualHeight;
}
If i take a existing TextBlock in XAML and just:
MyXAMLTextBlock.MaxWidth = 7;
double height = MyXAMLTextBlock.ActualHeight;
I get the right value for it.
I know there are possibilitys with Meassure() and Arrange() like in this Example, but Meassure(Size) takes the size paramter and i dont have the height for it.
I know that you can set the size on Auto with Double.Nan, but it doesnt compile with
double height = 9;
Size s = new Size(height, Double.Nan);
Maybe you can help me and thanks a lot.
I have no idea why you are doing this. But with Measure and Arrange it is possible:
Just run this two lines before you return the ActualHeight:
textblock.Measure(new Size());
textblock.Arrange(new Rect());
Hope I could help you

Program crashes after adding a Border to a Textblock

I have a little problem. I am enabling the user to choose the size of a textblock in which he is able to display text by doing some other stuff.
My problem here is that I have to add a border to the textblock to show the user how big it became.
When I applied the following code, my program just crashes in that scenario:
//create a TextBlock at desired position
TextBlock tmpTextBlock = new TextBlock
{
Width = 166,
Height = Math.Max(tmpY1, tmpY2) - Math.Min(tmpY1, tmpY2),
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(0, Math.Min(tmpY1, tmpY2) - 50, 0, (int)WeekGrid.ActualHeight - Math.Max(tmpY1, tmpY2)),
Text = "Type stuff here"
};
tmpTextBlock.Holding += tmpTextBox_Holding;
tmpTextBlock.RightTapped += tmpTextBox_RightTapped;
WeekGrid.Children.Add(tmpTextBlock);
Grid.SetRow(tmpTextBlock, 1);
//add the border - these lines produce the problem
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
};
The Exception that follows is an argument exception:
Value does not fall within the expected range.
EDIT
Whoops I've solved that problem. I had to remove adding the Textblock into the grid.
The problem I have now is that the border appears around the grid - not around the textblock!
The following code made that possible:
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
Padding = new Thickness(24)
};
WeekGrid.Children.Add(border);
Grid.SetRow(border, 1);
EDIT 2
Problem solved again.
I of course had to remove the margin setting of the textblock!
Thank you very much!
Greetings,
FunkyPeanut
Could you post the exception?
clearly there is an error in your code here:
WeekGrid.Children.Add(tmpTextBlock);
Grid.SetRow(tmpTextBlock, 1);
//add the border - these lines produce the problem
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
};
The component Border it is like a "container", which accepts a single element, you should switch to:
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
};
WeekGrid.Children.Add(border);
Grid.SetRow(border, 1);
You should also check if the resources are "available" for your access.

Setting an overlay background panel on a dynamic Grid

Right, so I'm drawing what looks like a DataGrid on a standard WPF Grid panel.
The content controls are essentially TextBlock controls inside Border controls at Grid row and column references to create the overall layout, added at runtime and using the usual Grid.SetRow() and Grid.SetColumn() methods
Not every X and Y grid reference has a content control - this is determined by a runtime definition of the data for the grid. I use offset values to mark whether an XY pair is part of the "headers" of the rows/columns or whether the grid reference is part of the "data" section.
I am trying to put a gradient background across the whole of the "data" section. I was using a Grid control with the gradient brush set as follows:
//Grey background to show for "grey cells"
Grid greyBackground = new Grid()
{
Background = (Brush)this.Resources["BackgroundBrush"],
VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
Width = double.NaN,
Height = double.NaN,
};
greyBackground.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
greyBackground.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
Grid.SetColumn(greyBackground, _vm.Definition.ColumnOffset);
Grid.SetRow(greyBackground, _vm.Definition.RowOffset);
Grid.SetColumnSpan(greyBackground, _vm.Definition.Columns - _vm.Definition.ColumnOffset);
Grid.SetRowSpan(greyBackground, _vm.Definition.Rows - _vm.Definition.RowOffset);
cellGrid.Children.Add(greyBackground);
This however is not visible at all. And I have no idea why.
Any idea how I can display a gradient background painted control over these row/column locations, underneath the other "data" controls?

How to calculate font size of TextBlock to fill in Canvas?

What I need is some formula to calculate font size of TextBlock for its owner - Canvas.
Let's say I have Canvas height 100.0 then which TextBlock font size should be to fill all space of the Canvas?
P.S. The main problem is that I scroll those TextBlocks horizontally...
Why not just use whatever FontSize while putting the TextBlock in a Viewbox whose Height is bound to that of the Canvas? (When not set explicitly the ActualHeight of the Canvas)
I found the solution which works fine for me.
double h = canvas1.Height / 2;
TextBlock1.FontSize = h;
I found the solution which works fine for me.
double h = canvas1.Height / 2;
foreach (var item in textBlocks)
{
if (item is TextBlock)
{
(item as TextBlock).FontSize = h;
}
}

Categories

Resources