I'm trying to bind two <Run>s inside a TextBlock as shown in the snippet below. But I'm getting an XamlParseException.
Basically I'm trying to achieve this format:
CodeNum: LongDescription
If the below code is doomed to fail what other alternatives do I have?
<TextBlock>
<Run FontWeight="Bold" Text="{Binding CodeNum}"/>
<Run FontWeight="Bold" Text=": "/>
<Run Text="{Binding LongDescription}"/>
</TextBlock>
I'm guessing that either LongDescription or CodeNumis is a read-only property (doesn't have public setter). You need to change binding to be one way for all read-only properties that you use in Run
<Run Text="{Binding LongDescription, Mode=OneWay}"/>
Related
I have Button with Resources:
Content="{x:Static resPath:Resources.ZoomIn}"
which value = "Zoom in". I want to add to this content " x1". To get value = "Zoom in x1".
I want do something like this:
Content="{x:Static resPath:Resources.ZoomIn} + x1"
Content="{x:Static resPath:Resources.ZoomIn} + {x1}"
But it throw error.
Use a TextBlock as Content:
<Button>
<TextBlock>
<Run Text="{x:Static resPath:Resources.ZoomIn}"/>
<Run Text="x1"/>
</TextBlock>
</Button>
or
<Button>
<TextBlock>
<Run Text="{x:Static resPath:Resources.ZoomIn}"/> x1
</TextBlock>
</Button>
You can use a TextBlock with a Binding and StringFormat to format the bound text.
<Button>
<TextBlock Text="{Binding Source={x:Static resPath:Resources.ZoomIn}, StringFormat={}{0} x1}"/>
</Button>
The {} is an escape sequence, {0} just refers to the bound value (when binding multiple values with e.g. a MultiBinding you could access more than one) and the rest is the your custom text.
I'm building a Universal Windows App and using the ListView, upon click I can't seem to get the selected item text/item details in general..
My XAML Code:
<ListView Name="listviews" Margin="10,172,10,0" Tapped="listviews_Tapped" SelectionChanged="listviews_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Bookings.ID}" Visibility="Collapsed" />
<TextBlock>
<Run Text="{Binding Bookings.first}" FontSize="24"></Run>
<Run Text="{Binding Bookings.last}" FontSize="32"></Run>
</TextBlock>
<TextBlock Tag="{Binding Bookings.ID}">
<Run Text="Total: "/>
<Run Text="{Binding Bookings.pax}" />
<Run Text=" - M: "/>
<Run Text="{Binding Bookings.total_male}"></Run>
<Run Text=" / F: "/>
<Run Text="{Binding Bookings.total_female}"></Run>
</TextBlock>
<ToggleSwitch x:Name="toggleSwitch1" OnContent="Checked-in" OffContent="Waiting" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I tried using Tapped & SelectionChanged but can't seem to grab the data... Any suggestions?
Thanks!
If you want to catch it on ItemClick event , you must set ListView's ItemClickEnabled property to True and SelectionMode to none.
After that , in the second parameter of ItemCLick event handler (e parameter usually) you can catch your model with e.ClickedItem property.
Don't forget to cast it to your Model like :
var myClickedItem = e.ClickedItem as Bookings
Please try to add this to your Page.cs file:
void listviews_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Bookings SelectedBook = (Bookings)listviews.SelectedItem;
var abc = SelectedBook.ID;
}
Now you can access all properties of Bookings-typed object using SelectedBook.xyz
Could anyone please let me know how I can display numbers like 100 to 10², with base 10 raised to the power of "n". The base-10 numbers are dynamic, so I cannot rely on the ASCII coding.
This is for a C# based WPF application.
You can use the Superscript value on the Run.BaselineAlignment property:
<TextBlock FontSize="14">
<Run Text="25" /><Run Text="2" FontSize="8" BaselineAlignment="Superscript" />
</TextBlock>
You can also data bind to the Run.Text property if you prefer:
<TextBlock FontSize="14">
<Run Text="{Binding Number}" /><Run Text="{Binding Power}" FontSize="8"
BaselineAlignment="Superscript" />
</TextBlock>
Also using this method, you can set the various properties individually on the number and the power, so you can get the exact look that you want.
I am trying to make a single block of text composed of multiple TextBlocks
- THE TARGET
For example, I want to achieve like the line below:
"I read this line in a ListBox, notice the multiple text formatting"
The way I'm trying to do is
<StcakPanel Orientation="Horizontal" Width="400" >
<TextBlock Text="I read this line in a " TextWrapping="Wrap" />
<TextBlock Text="ListBox" FontStyle="Italic" TextWrapping="Wrap"/>
<TextBlock Text=", notice the multiple " TextWrapping="Wrap" />
<TextBlock Text="text formatting" FontWeight="Bold" TextWrapping="Wrap"/>
<StcakPanel>
- THE PROBLEM
The text does not fit in the StackPanel, despite of setting TextWrapping for TextBlocks and Width for StackPanel.
I want to generate this code at runtime. I don't know how many words do I need to format.
Kind of showing SearchResults with highlighted search keywords.
- THE QUESTION
How can the above target be achieved either using StackPanel or something else? Having the following constraints.
The text length is unknown
The number of textblocks in the stackpanel is unknown
width and height is Unknown
Thanks very much
You should use a single <TextBlock> which can contain multiple <Run>s that can each have their own formatting. If you want to insert a linebreak, you can use the <Linebreak /> control.
<StackPanel Orientation="Horizontal" Width="400" >
<TextBlock>
<Run Text="I read this line in a" />
<Run Text="ListBox" FontStyle="Italic" />
<Run Text=", notice the multiple" />
<Run Text="text formatting" FontWeight="Bold" />
</TextBlock>
<StackPanel>
At that point you probably don't even need the <StackPanel> unless you are going to have multiple <TextBlocks> stacked on top of one another.
See this post for more information and examples: http://www.danderson.me/posts/working-with-the-wpf-textblock-control/
To databind multiple runs within a TextBlock, see this answer: Databinding TextBlock Runs in Silverlight / WP7
Given the below TextBlock, how do I make the SomeString part of the text bold?
<TextBlock Text="{Binding SomeString,StringFormat='{}Row: {0}'}" />
ie: If SomeString = "ABC" I want the TextBlock to look like this:
Row: ABC
Try something like this
<StackPanel Orientation="Horizontal">
<TextBlock Text="Row:"/>
<TextBlock FontWeight="Bold" Text="{Binding SomeString}"/>
</StackPanel>
Basically, you could format each individual Run inside the same TextBlock.
Through XAML
<TextBlock>
<Run>Row:</Run>
<Run FontWeight="Bold" Text="{Binding SomeString}"></Run>
</TextBlock>
MSDN Section
Hope this helps.