Get largest width of a textblock in a stackpanel - c#

On a button click, I am reading through a CSV file, replacing ',' with '\t' and writing it out to a stackpanel.
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < r.variables.Count; i++)
{
_people.Add(new TextBlock() { Text = r.variables[i], HorizontalAlignment = System.Windows.HorizontalAlignment.Right });//.ToString() });
StackPanel stp = new StackPanel() { Orientation = Orientation.Vertical };
TextBlock tb = new TextBlock() {Text = r.variables[i]};
stp.Children.Add(tb);
_secondStack.Children.Add(stp);
}
foreach (StackPanel sp in _secondStack.Children)
{
foreach (TextBlock tb in sp.Children)
{
Size desiredSize = new Size();
tb.Measure(this.availableSize);
desiredSize = tb.DesiredSize;
}
}
}
From the file, some contain strings that are longer than others, thus the TextBlocks in the header are wider than those TextBlocks below.
How do I get the width of the widest TextBlock in the embedded StackPanel and set the width of all TextBlocks inside the embedded StackPanel to that?

Once the StackPanel is populated, this is how you get the largest Width and set it for all other TextBoxes:
double largestWidth = stackPanel.Children.OfType<TextBox>().OrderByDescending(
textbox => textbox.ActualWidth).First().ActualWidth;
foreach (var textBox in stackPanel.Children.OfType<TextBox>())
{
textBox.ActualWidth = largestWidth;
}

Related

C# - Adding new Textblocks to Grid RowDefinition programmatically

I am trying to make an application which reads out RSS feeds and shows them in an WPF form. To separate the posts I am using a grid and add 2 more RowDefinitions (one for the title and one for the subject).
Currently I am stuck. I want to add some Textblocks to the Rows that I just defined, but I have no idea how because they don't have names. Does anyone have an idea?
What I currently have:
//defining Grid
Grid post = new Grid();
post.HorizontalAlignment = HorizontalAlignment.Left;
post.VerticalAlignment = VerticalAlignment.Top;
post.Margin = new Thickness(10);
post.ShowGridLines = true;
post.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(500)
});
//loop trough all RSS feeds
foreach (SyndicationItem item in feed.Items)
{
//add rows for title and Summary
post.RowDefinitions.Add(new RowDefinition()
{
Height = new GridLength(50),
});
post.RowDefinitions.Add(new RowDefinition()
{
Height = new GridLength(50)
});
//Grid.SetRow(TitleTextblock, 0);
//Grid.SetRow(SummaryTextblock, 1);
//fills textblocks
post.Children.Add(new TextBlock()
{
Text = item.Title.Text
});
post.Children.Add(new TextBlock()
{
Text = item.Summary.Text
});
}
//show grid on window
this.Content = post;
which results in:
this
Allright, it looks like a problem with setting your Definitions correctly:
//defining Grid
Grid post = new Grid();
post.HorizontalAlignment = HorizontalAlignment.Left;
post.VerticalAlignment = VerticalAlignment.Top;
post.Margin = new Thickness(10);
post.ShowGridLines = true;
post.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(500)
});
//add local field to keep track of what row you're on
int rowdeff = 0;
//loop trough all RSS feeds
foreach (SyndicationItem item in feed.Items)
{
//add rows for title and Summary
post.RowDefinitions.Add(new RowDefinition()
{
Height = new GridLength(50),
});
post.RowDefinitions.Add(new RowDefinition()
{
Height = new GridLength(50)
});
//create the textblocks
TextBlock TitleTextblock = new TextBlock;
TextBlock SummaryTextblock = new TextBlock;
//add content to textblocks
TitleTextBlock.Text = item.Title.Text;
SummaryTextblock.Text = item.Summary.Text;
//set definitions
Grid.SetColumn(TitleTextblock, 0);
Grid.SetColumn(SummaryTextblock, 1);
Grid.SetRow(TitleTextblock, rowdeff);
Grid.SetRow(SummaryTextblock, rowdeff);
//fills textblocks
post.Children.Add(TitleTextBlock);
post.Children.Add(SummaryTextblock);
//add next row
rowdeff++;
}
//show grid on window
this.Content = post;
You can try
TextBlock textBlock = new TextBlock();
Grid.SetRow(textBlock, 0);
Grid.SetColumn(textBlock, 0);
Hope this helps you

Width for wpf Textblock inline elements

Is it possible to give width for a textblock inline element in WPF? I am using Run to add inline elements to text block. I want each run to be of same fixed size.But I couldn't find width property for Run.
For example
TextBlock txtblck = new TextBlock();
txtblck.Inlines.Add(new Run() { Text = "abc" });
txtblck.Inlines.Add(new Run() { Text = "def" });
I want both the Runs to be of same width. Is it possible? Please help me.
Thanks in advance.
You can use InlineUIContainer:
TextBlock txtblck = new TextBlock();
txtblck.Inlines.Add(new InlineUIContainer
{
Child = new TextBlock
{
Text = "abc",
Width = 100
}
});
txtblck.Inlines.Add(new InlineUIContainer
{
Child = new TextBlock
{
Text = "def",
Width = 100
}
});

Creating XAML grid rows programmatically

I'm trying to generate a grid of data programmatically.
private void WorkPackageSearchButton_Click(object sender, RoutedEventArgs e)
{
EWP[] workPackages = SqlDataLayer.getSearchedWorkPackages(WorkPackageSearchBox.Text);
//declare variables
RowDefinition RowDef;
StackPanel StackP;
TextBlock TextB;
TextBlock TextC;
for (int i = 0; i < workPackages.Length; i++)
{
//Define new Row to add
RowDef = new RowDefinition();
RowDef.Height = new GridLength(30);
//Add row definition to Grid
WorkPackageResults.RowDefinitions.Add(RowDef);
//Define the control that will be added to new row
TextB = new TextBlock();
TextB.Text = workPackages[i].EWPStatus;
//TextB.Width = 75;
TextC = new TextBlock();
TextC.Text = workPackages[i].EWPCode;
//TextC.Width = 175;
//create stackpanel and define which row to add the stackpanel to
StackP = new StackPanel();
StackP.SetValue(Grid.RowProperty, i);
//add your control to the stackpanel
StackP.Children.Add(TextB);
StackP.Children.Add(TextC);
//add the stackpanel to the grid
WorkPackageResults.Children.Add(StackP);
}
}
The result is the data being added on top of each other:
How do I get each of the textblocks per iteration of the loop to be next to each other in a single row?
You have to set the Orientation of the StackPanel you're using to Horizontal
StackP = new StackPanel();
StackP.Orientation = Orientation.Horizontal;
the default Orientation of a StackPanel is Vertical, so it displays the items rows based.
Msdn Link and Examples

Set the minimum width for content control in an expander when collapsed

I have a pdfViewer with an expander wrapped around it. I am trying to set the minimum height of content control inside expander when collapsed to 250 and when expanded set the height to the page height.
so even if the user collapses the expander he can see a part of the document. There is no property to set the height of content control and the expander's height property as set in code below is not working.
Is there any way of achieving this?
private void exPDF_Expanded(object sender, RoutedEventArgs e)
{
if (PDFCaption != null)
{
PDFCaption.Text = "Click to hide PDF.";
pdfViewer.Height = this.ActualHeight;
exPDF.Height = this.ActualHeight;
var rowIndex = Grid.GetRow(sender as Expander);
var Row = LayoutRoot.RowDefinitions[rowIndex];
Row.Height = GridLength.Auto;
}
}
private void exPDF_Collapsed(object sender, RoutedEventArgs e)
{
if (PDFCaption != null)
{
PDFCaption.Text = "Click to show PDF.";
pdfViewer.Height = 250;
exPDF.Height = 250;
var rowIndex = Grid.GetRow(sender as Expander);
var Row = LayoutRoot.RowDefinitions[rowIndex];
Row.Height = GridLength.Auto;
}
}

Getting selected value of listbox windows phone 7

I am trying to get the selected value from a list box on the Windows Phone 7 platform. The data in my listbox is made up of three columns, consisting of 2 text blocks and 1 image object.
How should i put the code in the way that i can get the text (The data in any of the text block) of the selected one?
Below is my code for defining the grid:
//Define grid column, size
Grid schedule = new Grid();
foreach (var time in timeSplit)
{
timeList = time;
//Column 1 to hold the time of the schedule
ColumnDefinition scheduleTimeColumn = new ColumnDefinition();
GridLength timeGrid = new GridLength(110);
scheduleTimeColumn.Width = timeGrid;
schedule.ColumnDefinitions.Add(scheduleTimeColumn);
//Text block that show the time of the schedule
TextBlock timeTxtBlock = new TextBlock();
timeTxtBlock.Text = time;
//Set the alarm label text block properties - margin, fontsize
timeTxtBlock.FontSize = 28;
timeTxtBlock.Margin = new Thickness(0, 20, 0, 0);
//Set the column that will hold the time of the schedule
Grid.SetColumn(timeTxtBlock, 0);
schedule.Children.Add(timeTxtBlock);
}
foreach (var title in titleSplit)
{
titleList = title;
//Column 2 to hold the title of the schedule
ColumnDefinition scheduleTitleColumn = new ColumnDefinition();
GridLength titleGrid = new GridLength(500);
scheduleTitleColumn.Width = titleGrid;
schedule.ColumnDefinitions.Add(scheduleTitleColumn);
//Text block that show the title of the schedule
TextBlock titleTxtBlock = new TextBlock();
if (title.Length > 10)
{
string strTitle = title.Substring(0, 10) + "....";
titleTxtBlock.Text = strTitle;
}
else
{
titleTxtBlock.Text = title;
}
//Set the alarm label text block properties - margin, fontsize
titleTxtBlock.FontSize = 28;
titleTxtBlock.Margin = new Thickness(60, 20, 0, 0);
//Set the column that will hold the title of the schedule
Grid.SetColumn(titleTxtBlock, 1);
schedule.Children.Add(titleTxtBlock);
//scheduleListBox.Items.Add(schedule);
}
foreach (var category in categorySplit)
{
categoryList = category;
//Column 3 to hold the image category of the schedule
ColumnDefinition categoryImageColumn = new ColumnDefinition();
GridLength catImgnGrid = new GridLength(70);
categoryImageColumn.Width = catImgnGrid;
schedule.ColumnDefinitions.Add(categoryImageColumn);
TextBlock categoryTxtBlock = new TextBlock();
categoryTxtBlock.Text = category;
//set the category image and its properties - margin, width, height, name, background, font size
Image categoryImage = new Image();
categoryImage.Margin = new Thickness(-50, 15, 0, 0);
categoryImage.Width = 50;
categoryImage.Height = 50;
if (category == "Priority")
{
categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/exclamination_mark.png", UriKind.Relative));
}
else
if (category == "Favourite")
{
categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/star_full.png", UriKind.Relative));
}
Grid.SetColumn(categoryImage, 2);
schedule.Children.Add(categoryImage);
}
scheduleListBox.Items.Add(schedule);
}
Code for selected value of listbox:
string selectedName;
private void scheduleListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
//Get the value of selected value in scheduleListBox
if (null != scheduleListBox.SelectedItem)
{
selectedName = (scheduleListBox.SelectedItem as ListBoxItem).Content.ToString();
}
MessageBox.Show("Selected name : " + selectedName);
}
ListBoxItem.Content is the Grid you added to ListBox.Items. Then you can access Grid.Children to get added TextBlocks, resp. their Text properties.
Above is a formal answer. On another note and despite your code containing lots of white spaces, I don't believe that it can work. For example you're adding several images (textblocks) into a single grid cell. Is that intended? I don't think so. Didn't you want to use listbox itm with just one date (is it a date?), one title and one image? If so, change your logic.
ben tan !
you can get Tab of control :
Example:
string a = "abc"
grid myGrid = new grid();
myGrid.Tag = a;
when selectionChange you get Tab in control Grid ?

Categories

Resources