Custom fontsize for each pivotiem of a pivot - c#

In my project of a Windows Phone 7 app, I use some Pivot, and I would like to know if it is possible to change the fontsize of the header of a pivotitem in c#.
For the moment, I know how to change all of the fontsize of the pivot. Here how I do it <controls:Pivot x:Name="EventPivot">
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock x:Name="header" Text="Salut" FontSize="30" />
</DataTemplate>
</controls:Pivot.HeaderTemplate>
</controls:Pivot>
But my problem is that I didn't find how to change the Header text in c#. I tried
TextBlock headerText = new TextBlock { Text = "toto", FontSize= 20, TextWrapping = TextWrapping.Wrap};
var myPivotItem = new PivotItem { Header = headerText};
myPivotItem.Content = myScrollViewer;
EventPivot.Items.Add(myPivotItem);
But that didn't work.
Maybe if I use some sort of DataTemplate as I did for the Pivot I could solve my problem but I don't know how to implement it in c#...
I wonder also if the height of the PivotItem could be independant from all other PivotItems. Because for the moment, the height of each header is based by the larger height of the others PivotItems.
Thanks for all of your answers.
Bye
Drhouse87

Before implementing such a thing I'd test very carefully what the users think of such varying header item sizes. Not also that you'll likely have spacing issues as a result of this as the space reserved for the header will be applied to all pivot items.
That said, it should be possible to do this by binding a value to use for the FontSize and then applying a converter if need be.
You can't refer to items within a template by name from the code behind of the page as there could be multiple instances of the control wiht that name.

Related

Autosize to fit All Content

In WPF, I have the following:
<ListView.View>
<GridView>
<GridViewColumn
Width="Auto"
DisplayMemberBinding="{Binding SomeProperty, Mode=OneWay}"
Header="Type"/>
The problem is that this only auto-sizes the column to the visible content. This is the same behavior when double clicking the header divider as well.
I want it to resize the column according to all content. For contrast, take the Winforms DataGridView equivalent to the current Auto resizing behavior and the equivalent to the desired behavior:
this.dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; // Current behavior
this.dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; // Desired behavior
Is this possible for a WPF ListView?
This question is different than How to autosize and right-align GridViewColumn data in WPF? and others like it because they still trigger the current resizing behavior for visible content and do not address all content.
The problem is the ListView has its ItemsPanel with virtualizing enabled by default. In this mode, only visible content will be rendered and then will trigger the auto-sizing column. So if you don't have many items in your ListView, you can turn off the virtualizing mode via the attached property VirtualizingStackPanel.IsVirtualizing, then it will work expectedly:
<ListView VirtualizingStackPanel.IsVirtualizing="false">
<!-- ... -->
</ListView>
If you don't want to turn off virtualizing, then I don't think there is some easy way to take benefit of the Width="Auto". You may have to implement that feature manually yourself. And technically we need to have knowledge about the largest width to update the column's width accordingly. We can make some for loop every time we need to update the column's width but this is expensive. We can also manage to store the largest width every time a cell has its content changed (this is the better approach). Anyway doing it manually is a pain.

Add textblock to bottom of listview

I need to put a textblock containing result count information at the bottom of a databound listview. It needs to be within the listview's scrollbars but not be affected by the scrollbars (it must always sit at the bottom of the listview).
Because a listview cannot contain a textblock directly I am achieving this by adding some padding at the bottom of the listview and using a negative margin to make a separate textblock appear as though it is part of the listview. The issue with this is that when the listview's horizontal scrollbar is displayed it covers the textblock. I could add code to figure out if the scrollviewer is displayed and then adjust the margins/paddings accordingly BUT at this stage it sounds like a hacky solution.
Is there a better way to achieve this?
Instead of using a ListView.. you can do something like this:
<ScrollViewer>
<StackPanel>
<ItemsControl ItemsSource="{Binding MyCollection}" />
<TextBlock />
</StackPanel>
</ScrollViewer>

Change text of textblock within a panoramaitem

I need to use two textblock within a single panorama item. I have used a grid to implement this.
Problem is that I need to change the text of the textblock programmatically.
<phone:PanoramaItem Header="Current Status">
<Grid >
<TextBlock x:Name="Spent" Margin="138,0,90,464" FontSize="40"/>
<TextBlock x:Name="Left" Margin="138,50,90,414" FontSize="40"/>
</Grid>
</phone:PanoramaItem>
Saying:
Spent.Text = Convert.ToString(total_spent);
Left.Text = Convert.ToString(total_left);
in the c# file gives an error.
Please tell me how to change the text of the individual textblocks :)
P.S:
I am an absolute beginner and almost completely self taught, so a simple answer will be useful
Thanks
You need to use Text property of the TextBlock to get/set the Text.
From MSDN : TextBlock.Text
Gets or sets the text contents of a TextBlock.
Try This:
Spent.Text = Convert.ToString(total_spent.Text);
Left.Text = Convert.ToString(total_left.Text);

TextBlock won't display the rest of it's text even when it's resized

An app I'm working on retrieves information from the Internet and displays it in-app in a series of Grids inside of a StackPanel, itself nested inside of a ScrollViewer.
My problem is this: The app shows a short summary of the content, and the idea is that clicking the content makes the Grid and TextBlock expand to show the rest of the content, however resizing the TextBlock doesn't show the text that was cut off by being too short originally. The TextBlock is created dynamically in c# code.
Additionally, but not vital: I don't know how to get the full required height from the TextBlock to change it's text to, and without it I have to set the new size to a static number, which I feel detracts from the UX of my app.
Thanks,
Jase
Update: This is the code used to generate the TextBlock:
this.message = new TextBlock()
{
VerticalAlignment = VerticalAlignment.Top,
TextWrapping = TextWrapping.Wrap,
MaxWidth = getWidth()
};
if(post.message != null) { message.Text = post.message; }
Update 2: This is the code that creates the StackPanel (XAML):
<ScrollViewer Grid.Column="1" Style="{StaticResource MainColumn}" VerticalScrollBarVisibility="Auto" SizeChanged="Feed_SizeChanged">
<StackPanel x:Name="Feed"/>
</ScrollViewer>
This is where the parent Grid is generated:
Post grid = new Post() { Margin = new Thickness(0, 0, 0, 5) };
Feed.Children.Add(grid);
grid.layoutPost(oldCount, count--, post, Feed, this);
The height of the Grid is set in the layoutPost method, and ends up at 75px, Post is a class that extends Grid, and is defined here:
class Post : Windows.UI.Xaml.Controls.Grid
If you leave the Height of the TextBlock undefined and set TextWrapping="Wrap" - the TextBlock should automatically grow to the size required to fit its Text.
Another problem you might encounter is if you are trying to put large quantities of these TextBlocks in your ScrollViewer. That might not perform too well since you are not virtualizing your content and loading that much text requires a lot of CPU power for the layout. Usually using a ListBox or a ListView is a better choice for such scenarios since they use a VirtualizingStackPanel to lay out their child elements.

Setting width, height of TextBlock behind code for variablesizedwrapgrid not displaying correctly

I need some help confirming I'm seeing a bug.
I am creating a "TagCloud" in xaml for metro. Nothing complex, it should be simple.
I create TextBlock objects in the code behind and then add them to the children of a VariableSizedWrapGrid
Each TextBlock is a different height and width - and this is determined by the testblock.Measure(size), then setting the width and height via DesiredSize.Width and DesiredSize.Height
testblock.Measure(new Size(10000, 250));
dWidth = Double.Parse(testblock.DesiredSize.Width.ToString());
dHeight = Double.Parse(testblock.DesiredSize.Height.ToString());
The problem is, when the TextBlock objects are added they basically clip, they all lock to one single size (basically the behavior of a WrapGrid). Essentially the VariableSizedWrapGrid is refusing to allow variable heights and widths within its child elements? I have no idea why it clips everything.
<VariableSizedWrapGrid x:Name="vswg" Orientation="Horizontal" Width="Auto" Height="200" Grid.Column="1"/>
IEnumerable<TextBlock> textBlocks =
(from cloudItem in cloudItems
select ConvertToTextBlock(cloudItem, fontWeight));
vswg.Children.Clear();
foreach (TextBlock t in textBlocks)
{
vswg.Children.Add(t);
}
The ConvertToTextBlock() returns a new TextBlock with a different text and FontSize
I cannot get the VariableSizedWrapGrid to actually have a "variable sized" child control. Perhaps I am just using this all wrong?
Try Something like this:
<GroupStyle.Panel> <ItemsPanelTemplate > <VariableSizedWrapGrid x:Name="MyGrid" Orientation="Horizontal" MaximumRowsOrColumns="7"
ItemWidth="{Binding DataContext.ItemTemplateWidth, ElementName=pageRoot}"
ItemHeight="{Binding DataContext.ItemTemplateHeight, ElementName=pageRoot}"
></VariableSizedWrapGrid> </ItemsPanelTemplate> </GroupStyle.Panel>
Also consider reading:
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2012/09/14/windows-8-xaml-and-displaying-multiple-sized-items.aspx
http://www.acnenomor.com/3756161p1/how-to-display-gridview-items-with-variable-width-in-window-8
Try considering using DataTemplate.
VariableSizedWrapGrid provid ItemHeight and ItemWidth properties. Then, you can only choose the Width and Height of an item "in cells". So, you can choose your item to fit 3 cells width and 1 cell height.
You should consider reading: http://blogs.u2u.be/diederik/post/2012/03/07/Databinding-to-the-VariableSizedWrapGrid-in-Windows-8-Metro.aspx
Change this:
DesiredSize.Width.ToString()
To this:
Width.ToString()
The layout system is a complex three pass process, when you allow the "DesiredSize" value to be controlled by saying "DesiredSize in the code" the layout system takes charge. When you specify a width the layout system is overridden.

Categories

Resources