Textblock modify text automatically - c#

Let's say I have two TextBlock on the same page and both binding to the same variable:
<TextBlock Text={Binding [someViewModel].someText}/>
<TextBlock Text={Binding [someViewModel].someText}/>
//someText = "testingText"
I'm wondering if it's possible to have the first textblock to show "tes......" and the second textblock showing "testingText"?

Try this
<StackPanel>
<TextBlock Text="testingtext" MaxWidth="20" TextTrimming="CharacterEllipsis" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="testingtext" ></TextBlock>
</StackPanel>
output

Make an IValueConverter for one of the bindings which converts the String to the custom format you want.

Related

Long text in tooltip is not fully shown in UWP

I've defined a for a listbox item, and binded the text inside the tooltip to two properties of the object (name + description) but i have an issue that the text is being cut off
here is my tooltip:
<ToolTipService.ToolTip>
<StackPanel >
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13">
<Bold>Name</Bold>
</TextBlock>
<TextBlock Text="{x:Bind name}"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13" TextWrapping="Wrap">
<Bold>Description</Bold>
</TextBlock>
<TextBlock Text="{x:Bind description}"/>
</StackPanel>
</StackPanel>
</ToolTipService.ToolTip>
Now the thing is, if i bind the tooltip to a method that returns the name + description (Which is how it was previously, but was super ugly) it does show all the text, it was like this:
<ToolTipService.ToolTip>
<TextBlock Text="{x:Bind Description}"/>
</ToolTipService.ToolTip>
But I needed to style it to make it look better, so i've tried to do what was posted above.
I've already tried setting the Width/Height to super large values, didn't do anything.
any ideas?
The tooltip template probably has a default maximum width, which cuts off the TextBlock. To solve this, just add TextWrapping attribute:
<TextBlock TextWrapping="Wrap" Text="{x:Bind description}"/>
Now the tooltip text will wrap on multiple lines as necessary

Changing the Font of the string attached to the Bindings in WPF

Here is the XAML
<ListView x:Name="duplicateVarsInDepXMLListView" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="100" Margin="362,360,0,0" VerticalAlignment="Top" Width="306">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name, StringFormat='Variable Name: {0}'}"/>
<TextBlock Text="{Binding Value,StringFormat='Value: {0}'}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This xaml displays a List of collection with Variables Names and the values of a collection. We have something like Foreground property to change the colour of the binding characters or FontWeight to make it Bold.
However i only want to Change the font of the Label e.g. VariableName and not to the actual binding.
Is this possible to achieve? Any suggestions
You can use Run elements inside a TextBlock and apply different formatting to different runs. You can bind to the FontFamily property of the Run to dynamically set the font:
<TextBlock>
<Run Text="Variable Name: " FontFamily="{Binding FontName}"/>
<Run Text="{Binding Name}"/>
</TextBlock>
If the text in the TextBlock is wrapped using TextWrapping="Wrap" the runs will wrap around as expected. This does not apply to the question but can make using Run elements superior to stacking TextBlock elements in a StackPanel.
Extract out seperate TextBlock and set FontFamily on that TextBlock. Wrap both TextBlocks in StackPanel and set the orientation to Horizontal -
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Variable Name: " FontFamily="Comic Sans MS"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
<TextBlock Text="{Binding Value,StringFormat='Value: {0}'}"/>
</StackPanel>
You could do something like:
<StackPanel Orientation=Horizontal>
<TextBlock Text="Variable Name: "/>
<TextBlock Text={Binding Name} FontWeight="Bold"/>
</StackPanel>

Insert Hyperlink in TextBlock

In a WPF application I load news from an RSS feed. The contents are displayed in a TextBlock. This TextBlock has a certain size. Contents are cut off by the TextTrimming method.
Now I would like at the end of each TextBlock to insert a hyperlink button. The only problem is I do not know exactly what position will be cut on my string. Is there a way to figure this out?
When I insert my Text in my TextBlock and then my Hyperlink-Button, my HyperlinkButton will be cut of. Can I prevent to cut off my HyperlinkButton?
XAML-Code:
<TextBlock Name="myText" />
C#-Code:
Hyperlink hlink = new Hyperlink(new Run("here"));
myText.Inlines.Clear();
myText.Inlines.Add(value); //description from RSS Feed
myText.Inlines.Add(hlink);
Why not just add the HyperLink after the text, by replacing both items in a StackPanel?
If I understood your requirements this is one way to achieve your goals:
<StackPanel>
<DockPanel Width="200">
<TextBlock DockPanel.Dock="Left" Text="A short description." TextTrimming="CharacterEllipsis"/>
<TextBlock DockPanel.Dock="Right" TextAlignment="Right">
<Hyperlink NavigateUri="http://www.google.com">here</Hyperlink>
</TextBlock>
</DockPanel>
<DockPanel Width="200">
<TextBlock DockPanel.Dock="Left" TextTrimming="CharacterEllipsis" MaxWidth="170" Text="A really long descripion of the item." />
<TextBlock DockPanel.Dock="Right" TextAlignment="Right">
<Hyperlink NavigateUri="http://www.google.com">here</Hyperlink>
</TextBlock>
</DockPanel>
</StackPanel>
So the DockPanel control might be a good candidate to consider.
Slightly more convenient is to use a :
<TextBlock>
<Run Text="Short description"/>
<Hyperlink NavigateUri="http://www.google.com">here</Hyperlink>
</TextBlock>

C# & XAML - TextBox binding not updating content

I have a TextBox in a Stackpanel, as in the code below
<StackPanel x:Name="EtenStack" Visibility="{Binding Path=Sort, ConverterParameter=Eten, Converter={StaticResource convertEten}}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label Content="Bereidingstijd"/>
<TextBox Height="23" Width="150" Text="{Binding Path=Time, TargetNullValue='', Mode=TwoWay}"/>
</StackPanel>
When the Visibility is set to Visible in my converter, my textbox doesn't update it's Text property, even though the Property gets it's correct value (tested by showing a MessageBox with the Property).
Any thoughts?
The Time property will need to be either a Dependency Property with the right binding or on a class that Implements the INotifyPropertyChanged Interface, for the Time property, in order for the update to occur "Automatically."

Accessing a TextBlock which is contained inside a ListBox

I have a textblock which is inside a listbox and I am trying to write an if statement which is dependant on the contents of this textblock. I am trying to get the data from the TextBlack which I have named "category1" however when I try to write my if statement I am getting a message which just says
"the name category1 does not exist in the current context"
I tired moving that TextBLock out of the ListBox and it works fine but wont work while its inside there. Does anyone know how to reference this textblock.
Here is the my XAML code
<ListBox x:Name="HINList" Margin="0,300,-12,0" ItemsSource="{Binding Details}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding HINNumber}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding CategoryLetter}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="category1" Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Assuming you're writing your if statement in the code behind file, wouldn't something like:
if(((WhateverTypeIsInDetailsCollection)HINList.SelectedItem).Category1 == something) {
// then do whatever you want
}
As Russell pointed out there is a category1 item for every entry in the list. I assume you wanted to do something with the selected item.
This is due to xaml namescopes. The names inside a DataTemplate are in a different namescope than outside, that's why you can't access them (what #Russell pointed is part of why it's done this way).
I think that you want to access that field for the "Category1" property on the selected item of the HINList ListBox that is bound to the Details collection. What you can do is set the binding on the Category1 to be two way, and bind the SelectedItem of the ListBox to a Detail item like so:
xaml:
<ListBox x:Name="HINList" ItemsSource="{Binding Details}"
SelectedItem={Binding SelectedDetailItem, Mode=TwoWay}>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding Category1, Mode=TwoWay}" TextWrapping="Wrap" .../>
<!-- the other fields -->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
code-behind
if(SelectedDetailsItem.Category1==...)
{
....
}
Hope this helps :)

Categories

Resources