I use a two lines TextBlock. And I hope to wrap words in it, while each word's height
should match the height of TextBlock Height/2.
For example, the words may be "Hello Tom! Hello Jack! Hello Rose! Hello John!"
When they are in the TextBlock, they should look like below:
Hello Tom! Hello Jack!
Hello Rose! Hello John!
<Style TargetType=“TextBlock”>
<Setter Property=“TextWrapping” Value=“WholeWord” />
<!— Determine your TextBlock height and set half height here —>
<Setter Property=“LineHeight” Value=“50” />
<Setter Property=“MaxLines” Value”2” />
<!— Determine the width that gives you two likes of text to display how you’d like it —>
<Setter Property=“Width” Value=“100” />
<Setter Property=“TextAllignment” Value=“Left” />
<Style/>
Or follow the methods described here
Newline in string attribute
Related
With Xamarin, I have a small UI element which acts as a content divider:
<BoxView StyleClass="contentDivider"
HeightRequest="2"
WidthRequest="1000"
Margin="3, 0"/>
Since I use this a number of times I wanted to be able to have the code written down once, and reuse that code - just like a class with its instance (DRY). It's most likely me being a blind bat and not being able to find how it's done. So, how can I reuse XAML elements?
You can do this with ContentViews (https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/controls/layouts#contentview), which probably works better for larger reuse cases (using more XAML in the ContentView).
Yet, for such a small single element example as yours, you could really just consider using a global style (https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/application) which its looks like you already have with StyleClass="contentDivider", as long as you only want to override properties on a single element (like your BoxView).
Just add HeightRequest, WidthRequest and Margin to your style and your done.
<Style x:Key="contentDivider" TargetType="BoxView">
<Setter Property="HeightRequest" Value="20" />
<Setter Property="WidthRequest" Value="20" />
<Setter Property="Margin" Value="0,99,0,0" />
... etc
</Style>
I have the following:
<sys:String x:Key="NoDeviceAlert" xml:space="preserve">Your device is currently disabled.
Please ensure it is turned on and connected.</sys:String>
However it doesn't work. The TextBlock:
<TextBlock Text="{DynamicResource NoDeviceAlert}" Style="{DynamicResource msgTextStyle}" HorizontalAlignment="Center" />
keeps both sentences on a single line, but removes the decimal values. I have also tried hex values as well as /r/n.
How can this not work?
UPDATE
<Style x:Key="msgTextStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="#FFC8DBE7" />
<Setter Property="FontFamily" Value="/Project;component/Utilities/Resources/Fonts/frutiger.ttf#Frutiger Linotype" />
<Setter Property="FontSize" Value="20" />
<Setter Property="TextAlignment" Value="Left" />
</Style>
ResourceDictionary sys namespace:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
The application is for Windows desktop and using .net 4.5.
The attribute xml:space="preserve" affects only working of XML parser. So line breaks, tabs and spaces will stay preserved.
Instead of typing explicit newline chars just press enter:
<sys:String x:Key="NoDeviceAlert" xml:space="preserve">Your device is currently disabled.
Please ensure it is turned on and connected.</sys:String>
You can try use
&#x 0a;
like a line break (without this space inside).
It should be work.
I have a gridview and there're many gridviewitems in it, every gridviewitem contains some textblock, could I specify a style for all of the textblock at once? Not specify the style one by one?
You should be able to leverage the object resources and name it implicitly. Something like;
<GridView>
<GridView.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<!-- Etc, etc, etc -->
</Style>
</GridView.Resources>
</GridView>
I'm trying implicitly apply a style for DataGrid and TextBlocks.
For TextBlock's ForeGround I need White color.
For DataGrid's rows I need Black Color.
Beside this I need White again for DataGrid's header columns.
When I globally apply an implicit style for on MainPage by
<UserControl>
<UserControl.Resorces>
<Style targetType="TextBlock">
<Setter Property="Foreground" Value="White"/>
</Style>
</UserControl.Resorces>
</UserControl>
Making TextBlock's Foreground White operation is done! But beside this all of elements
in DataGrid (By default content elements are textblock's I think) turn to White color.
It doesn't look good White on white as you guess :)
So how can I particularly specify DataGrid's elements Foreground to black?
I can do it by using same technic shown below ,but this is an expensive operation for each DataGrid. As a con more I want DataGrid's HeaderColumns white again.This operation make them all black.
Is there an explicit way such as we do in css styles?
Here is what I tried to achieve this goal by Control template. But no chance because of being DataGrid's ContentControl is dynamic.
<DataGrid>
<DataGrid.Resources>
<Style targetType="TextBlock">
<Setter Property="Foreground" Value="Black"/>
</Style>
<DataGrid.Resources>
In fact we use Telerik's RadGridView but I give a sdk's DataGrid example to make question more global.
<Style TargetType="sdk:DataGrid">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="RowDetailsTemplate" Value="{StaticResource DataTemplate1}"/>
<Setter Property="Template" Value="{StaticResource ControlTemplate1}"/>
</Style>
<ControlTemplate x:Key="ControlTemplate1" TargetType="sdk:DataGrid">
<Grid/>
</ControlTemplate>
<DataTemplate x:Key="DataTemplate1">
<Grid/>
</DataTemplate>
Thanks in advance!
If it were me I would pull out the full control templates and style them accordingly instead of trying to just do adhoc setter changes to override bits of the original template. In Expression Blend right click, choose "Edit Template -> Edit A Copy" and break out the templates for your rows etc and apply those implicitly with StaticResource instead.
What is the correct way to apply styles on elements with a condition on their parent element's type, i. e. only if they are children of certain other elements?
In my case, I want to apply some exact button width and height, but only if those buttons are direct children of a stackpanel. Additionally, a second style should be applied to images within those buttons (glyphs).
How do I define a button style that only affects buttons on a stackpanel, but not those buttons placed directly on a grid?
Is it possible to add additional conditions such as only stackpanels with orientation="horizontal"?
Can I define "tree conditions" like only images on buttons on [horizontal] stackpanels?
As 90% of all buttons in my application are those on the stackpanels, so far I've applied the style to all buttons and images and overrode it where necessary. But this isn't the best solution, is it?
Preferably, the solution would deal with all the conditions in the style definition, so I won't have to explicitly assign that style to every single one of my stackpanels.
<StackPanel>
<StackPanel.Resources>
<Style x:Key="Rectangle1" TargetType="Rectangle">
<Setter Property="Stroke" Value="Black" />
<Setter Property="Fill" Value="White" />
</Style>
</StackPanel.Resources>
<UniformGrid Columns="10">
<UniformGrid.Resources>
<Style TargetType="Rectangle" BasedOn="{StaticResource Rectangle1}">
<Setter Property="Fill" Value="Red" />
</Style>
</UniformGrid.Resources>
</UniformGrid>
</StackPanel>