How does inheritance of FontSize in silverlight tree work? - silverlight

If I have a ChildWindow in Silverlight I can apply the FontSizeProperty and it is inherited by child items.
<controls:ChildWindow FontSize="14">
<StackPanel>
<TextBlock Content="Hello">
<TextBlock Content="World">
</StackPanel>
</controls:ChildWindow>
Now that's fine if you want the whole page to have the same font size, but I want to do something like this and have inheritance in smaller blocks:
<controls:ChildWindow>
<StackPanel FontSize="14">
<TextBlock Content="Hello">
<TextBlock Content="World">
</StackPanel>
<StackPanel FontSize="10">
<TextBlock Content="Hello">
<TextBlock Content="World">
</StackPanel>
</controls:ChildWindow>
This doesn't compile. Is there any way i can achieve this pattern in Silverlight without having to define a style for StackPanel (I think that would work).
Are there any other containers that let me set FontSize for its descendants - or can I write one that would?
I want to easily set fontsize to be larger for certain StackPanels. I don't want to resort to styles because its very specialized and I won't need to reuse the style elsewhere.
Whats the best way to do this?

You can wrap each StackPanel in a ContentControl, which does implement FontSize.

Related

WPF TextBox in DataTemplate of ToggleButton does not show text if in toolbar flyout

If I put the Column where the toolbar is hosted to be very big (800) then all the text is visible:
but if I put a smaller column this happens:
But I cannot understand why:
<DataTemplate x:Key="IconFilterButton">
<StackPanel Orientation="Horizontal">
<TextBlock
VerticalAlignment="Center"
Style="{StaticResource LargeIconStyle}"
Text="{Binding}" />
<TextBlock
Margin="6,0,0,0"
VerticalAlignment="Center"
DataContext="{Binding}"
Style="{StaticResource BodyTextStyle}"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToggleButton}, Path=Tag}" />
</StackPanel>
</DataTemplate>
and here the definition
<ToggleButton
x:Name="DFilter"
Click="Filtering_Click"
Content=""
ContentTemplate="{StaticResource IconFilterButton}"
Tag="1d"
/>
<ToggleButton
x:Name="WFilter"
Click="Filtering_Click"
Content=""
ContentTemplate="{StaticResource IconFilterButton}"
Tag="1w"
/>
Even worst if I click on the button once they are out:
and then the text is visible but is wrong as the TextBlock is not considered in the object size:
The WPF ToolBar control uses a custom panel for the overflow Popup. In many styles, the ToolBarOverFlowPanel has a property WrapWidth set to a static value like 200. This determines how many items can be displayed before it wraps to another row in the popup.
I've created custom styling for this control and have found that the ToolBarOverFlowPanel used internally is buggy. That's probably the source of your problem.
You can re-template the ToolBar and wire-up a different value for WrapWidth to try to fix the issue, but my guess is that you'll still run into layout problems.
Otherwise, you might consider making your own replacement control.

Silverlight TextBlock TextTrimming inside ContentControl disappears

I'm displaying a series of messages (like emails) on a Grid:
<layout:TransitioningContentControl Name="tccCmdMessage" Margin="0,4">
<layout:TransitioningContentControl.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" FontWeight="SemiBold" />
<TextBlock Name="tbCmdMessage" Text="{Binding Message}" TextTrimming="WordEllipsis" />
</StackPanel>
</DataTemplate>
</layout:TransitioningContentControl.ContentTemplate>
</layout:TransitioningContentControl>
However, the tbCmdMessage never displays. If I remove the TextTrimming (or change it to None) it works. Alternatively if I don't use a ContentControl parent it also works.
Any ideas?
Take a look at this link: http://social.msdn.microsoft.com/Forums/eu/wpf/thread/30fd3279-7bc8-424f-9ee6-41b9f9589a1a.
I suppose explicitly specifying the Width (or MaxWidth) of the StackPanel can make the texts trimmed. You can also try to use another type of container, like Grid.
Other links with similar issue described:
Silverlight text trimming and wrapping issue
TextTrimming not working
http://forums.silverlight.net/t/58227.aspx/1

Is it possible to bind to a control's property from a datatemplate?

Ok, sounds odd, and there's likely a better way, but I haven't seen it yet.
What I'm trying to do is restyle a ListPicker under Windows Phone 7.
What I need is to
get rid of the header (that's easy, just define a null ListPicker.HeaderTemplate).
Force the picker to always go to full mode when clicked (again, easy, just set the ItemCountThreshold to 1).
Restyle the itemtemplate used when in FullMode (again, easy, just define a FullModeItemTemplate)
Incorporate the ListPicker's "HEADER" property value into the ItemTemplate (since only one item will ever show, i need the header text "embedded" within the one item).
It's that number 4 that I can't seem to get.
I've defined a listpicker like so (i'm directly defining the templates inline instead of in resources for now, just to keep things simple).
<phonekit:ListPicker Header="Header Text" x:Name="ListOfSounds"
SelectedItem="{Binding Path=DepartureChime, Mode=TwoWay, Converter={StaticResource EnumDescriptionToStringConverter}}"
ItemCountThreshold="1">
<phonekit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="Transparent">
<TextBlock Text="{TemplateBinding Header}" />
<TextBlock Text="{Binding}" />
<TextBlock Text=">" />
</StackPanel>
</DataTemplate>
</phonekit:ListPicker.ItemTemplate>
Ignoring all the obvious formatting bits for now, the problem I'm having is that I can't use {TemplateBinding Header} from within a datatemplate. I've used it from a ControlTemplate no problem.
The result of this ItemTemplate should be an item displayed such as
{TextOfHeader}{Content of selected Item}>
I'm just not sure how to go about getting at a property of the templated control (the listpicker in this case).
Any ideas?
Take advantages of RelativeSource:
<TextBlock Text="{Binding Path=Header, RelativeSource={RelativeSource AncestorType={x:Type phonekit:ListPicker}}}" />

Is there something in WPF similar to Style.BasedOn for DataTemplate?

At the moment, I have two very large DataTemplate objects to display two sets of items in two ListBoxes. The DataTemplates are referenced in the ContentTemplate property in two Styles that are set in the ItemContainerStyle properties of the two ListBoxes. The items are of the same type and the DataTemplates are identical except for the following control:
From DataTemplate1
<TextBlock Style="{StaticResource TextStyle}" FontSize="20" Foreground="White"
HorizontalAlignment="Left" Panel.ZIndex="2" Text="{Binding RemainingTime.TotalHours,
Converter={StaticResource DoubleToIntegerConverter}, StringFormat={}{0:#00}}" />
From DataTemplate2
<TextBlock Style="{StaticResource TextStyle}" FontSize="20" Foreground="White"
HorizontalAlignment="Left" Panel.ZIndex="2" Text="{Binding ElapsedTime.TotalHours,
Converter={StaticResource DoubleToIntegerConverter}, StringFormat={}{0:#00}}" />
Is there some way to avoid duplicating the whole Dataemplate but still have this one difference in the text binding of this TextBlock in the second template?
No, there is no inheritance for DataTemplate. If you think about, how would you override a part of a DataTemplate?
Solution: Use another Style to capture the common properties between the two templates. You can scope it in the same Resources block if it only place you need it. It is much cleaner or more WPF way of doing things.
I've already asked this question here once and unfortunately there isn't.
but in this specific situation you can move the fontsize,foreground,horizontalalignment..etc to a style (lets say textstyle2) that based on your current textstyle.
I got an answer to this from another post (by Liz). Basically, you can put all common controls into one DataTemplate and then create two more DataTemplates that each use the first one as a ContentTemplate in a ContentPresenter. Then, you can add different controls into one or both of the latter DataTemplates. Liz provided a code example.
<DataTemplate x:Key="UserTemplate">
<!-- show all the properties of the user class here -->
</DataTemplate>
<DataTemplate DataType="{x:Type local:User}">
<ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource UserTemplate}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Author}">
<StackPanel>
<ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource UserTemplate}"/>
<!-- show all the additional Author properties here -->
</StackPanel>
</DataTemplate>
Thanks once again Liz.
Adding to what Dennis suggested, you can always create a custom control that you just stick inside your DataTemplate and re-style that control instead of the DataTemplate.

How can I factor out a DataTemplate's binding in WPF?

I have a DataTemplate I want to reuse. The part I want to factor out is the binding, because it's the only thing that changes. My DataTemplate looks roughly like this. (There's actually quite a bit more to it, but I've taken out the extraneous stuff.)
<DataTemplate>
<TextBox Text="{Binding Name}" />
</DataTemplate>
How can I reuse this DataTemplate while simply varying the property to which I'm binding? (Note that if it were as simple as just a TextBox, I wouldn't worry about it, but the DataTemplate actually contains a StackPanel with a number of other elements in it. I want to centralize that in one place, hence the DataTemplate.)
I've thought about two ways to tackle this problem.
Create a simple custom control. Reuse that, and don't worry about reusing the DataTemplate.
Experiment with some kind of subclass of DataTemplate. (I'm told this is possible.) I'd add a dependency property to it that lets me specify the name of the property to which I want to bind.
Suggestions?
I hate answering my own questions, but for the sake of completeness, here's my solution.
<ListBox ItemsSource="{Binding}">
<ListBox.Resources>
<ControlTemplate x:Key="textBoxControlTemplate" TargetType="ContentControl">
<TextBox Text="{TemplateBinding Content}" />
</ControlTemplate>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding Name}" Template="{StaticResource textBoxControlTemplate}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This of course is a very contrived example. In my own app, I'm not actually putting textboxes inside of a listbox. In a listbox, this is not very useful, but imagine it inside of a DataGrid, where each column might be displayed in a similar way, but binds to a different property.
Create a UserControl and use it within the DataTemplate.
<DataTemplate>
<local:MyComplexUserControl DataContext="{Binding Name}"/>
</DataTemplate>
and within the UserControl:
<StackPanel>
<TextBlock>Value:</Text>
<TextBox Text="{Binding}"/>
</StackPanel>
Have a separate DataTemplate with its own binding for each occasion.

Resources