Wpf control connect strings in Text/Content - c#

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.

Related

C# Windows Universal App ListView Selected Item Content

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

Binding Run inside Textblock results in exception in WPF

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}"/>

WPF - Tooltip on a disabled hyperlink with an embedded TextBlock element

I'm trying to put tooltips on disabled hyperlinks in my WPF app. The hyperlinks have embedded TextBlock elements for Text parameter binding. However, for some reason tooltips don't work on disabled hyperlinks with an embedded TextBlock element. Here is an example:
<Grid>
<StackPanel>
<TextBlock TextAlignment="Center" Margin="5">
<Hyperlink IsEnabled="False" ToolTip="ToolTip" ToolTipService.ShowOnDisabled="True">Text</Hyperlink>
</TextBlock>
<TextBlock TextAlignment="Center" Margin="5">
<Hyperlink IsEnabled="True" ToolTip="ToolTip" ToolTipService.ShowOnDisabled="True">
<TextBlock Text="Text"/>
</Hyperlink>
</TextBlock>
<TextBlock TextAlignment="Center" Margin="5">
<Hyperlink IsEnabled="False" ToolTip="ToolTip" ToolTipService.ShowOnDisabled="True">
<TextBlock Text="Text"/>
</Hyperlink>
</TextBlock>
</StackPanel>
</Grid>
This XAML describes three hyperlinks.
The first hyperlink is disabled, but has no embedded TextBlock element. The tooltip shows up fine.
The second hyperlink has an embedded TextBlock element, but is enabled. Again, the tooltip shows up fine.
The third hyperlink is disabled and has an embedded TextBlock element, which is what I need, but the tooltip is not shown.
What can I do to show tooltips on disabled hyperlinks with embedded TextBlock elements? I don't want to add the tooltip to the parent TextBlock, because I want the tooltip to only appear on the hyperlink text, and not the whole TextBlock area.
Thanks.
I know it sounds strange but this seems to work:
<TextBlock Text="Hello there" IsEnabled="False">
<Hyperlink ToolTip="ToolTip" ToolTipService.ShowOnDisabled="True">
<TextBlock Text="Text" />
</Hyperlink>
</TextBlock>
i.e., you have to disable the parent TextBlock.
Move Tooltip to textblock like this
<TextBlock TextAlignment="Center" Margin="5" ToolTip="ToolTip">
<Hyperlink IsEnabled="False" ToolTipService.ShowOnDisabled="True">
<TextBlock Text="Text"/>
</Hyperlink>
</TextBlock>

How to wrap multiple textbox wrap together?

I want to 1. from the following image:
My code is here:
<WrapPanel>
<TextBlock Text="Title: " Style="{StaticResource Title}" TextWrapping="Wrap" />
<TextBlock Text="{Binding Description" Style="{StaticResource Normal}" TextWrapping="Wrap" />
</WrapPanel>
But if Description text is short, shown like 2., if Description text is long, shown like 3.
How to do this like 1.?
I have solve my question using Run:
<TextBlock TextWrapping="Wrap">
<Run Text="Title: " Style="{StaticResource TitleRun}"/>
<Run Text="{Binding Description,Mode=OneWay}" Style="{StaticResource NormalRun}"/>
</TextBlock>
Just keep adding them to the Grid with 2 columns and n number of rows, adding new rows/colls as you add them.
You can create a behaviour for that.

How do I change the font on the bound part of a TextBlock's content?

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.

Categories