WPF highlighting - c#

Anyone has any idea how to highlight in a textblock?
Basically i have 2 textblock and both have the same string. When I highlight part of the string in one of the textblock, the other textblock shows the same highlighted part as well. I am basically stuck at how to do the highlighting.
Thanks in advance

Do you have a TextBlock or a TextBox? I'm going to assume it's a TextBox, since TextBlock does not support text selection.
In that case, you can simply use data binding to keep this in sync.
<TextBox Name="text1" />
<TextBox Name="text2"
SelectionStart="{Binding Path=SelectionStart, ElementName=text1}"
SelectionLength="{Binding Path=SelectionLength, ElementName=text1}" />
This should ensure that the same area of text is selected in text2 when the user selects it in text1 and vice-versa.
EDIT See this answer for instructions on how to bind to these properties.
If you create a custom TextBox as described in the linked answer, your code would look something like this:
<SelectionBindingTextBox Name="text1" />
<SelectionBindingTextBox Name="text2"
BindableSelectionStart="{Binding Path=BindableSelectionStart, ElementName=text1}"
BindableSelectionLength="{Binding Path=BindableSelectionLength, ElementName=text1}" />

Related

Hashtags in Binding TextBlock C#

I have a listview in my app (C# - UWP)
and in my DataTemplate i have a TextBlock that it Binding a text.
this is my code:
<TextBlock Text="{Binding Caption}" FontSize="11"/>
Now, how can i coloring all hashtags in the text? And clickable?
Note: all captions that binding this TextBlock is variable.
Like:
This is test #message for testing
Or
I like #German and #Russian language
I want change color #message, #German and #Russian and clickable feature in TextBlock
One option is to use a rich text box. Rich text box can render HTML like tags.
so you can have text like
<p> I am following the <a>#Russian-Language</a> <a>#azure</a> tutorials. </P>
Then anchor tags can have targets and they will be clickable. OR you can call a method on this hyperlink click.
Hope this helps you.
I made a control few months back called HashHandleTextBlock. The core concept of this is based on MarkdownTextBlock of UWP Community Toolkit.
Below is how you use controls.
<UnwantedControls:HashHandleTextBlock Text="{Binding ElementName=InputText, Path=Text}"
LinkForeground="DarkGray"
HashPrefix="https://twitter.com/hashtag/"
HandlePrefix="https://twitter.com/" />
You can also download the source from Github and modify control for your requirement.

List<String> Databind to multiple Textbox on WPF

Hi I have read some tutorials on Data Binding but I can't seem to find what I need which is a DataBinding of a List to a Multiple TextBox.
I'm not sure if this is possible on WPF and I'm quite new to Data Binding so bear with me.
I have a List(Global Variable)
and let say it has 3 words which are "Apple", "Banana" and "Orange"
this 3 words are given by the Code Behind of my XAML.
In my XAML I have 3 TextBox, from first to third I want to assign my List to it so from the
first textbox -> Apple
second textbox -> Banana
third textbox -> Orange
Now the catch here is that I need it to work in 2 ways which means for example
I edit the first textbox to "Mango", the List on the Code Behind will also change.
Is this possible on Data Binding?
You can just bind to the element of the list.
<TextBox Text="{Binding Path=FruitList[0]}" />
<TextBox Text="{Binding Path=FruitList[1]}" />
<TextBox Text="{Binding Path=FruitList[2]}" />
Edit:
The default binding method of a TextBox is TwoWay, but you can add this if you want to make it clear.
<TextBox Text="{Binding Path=FruitList[0]} Mode=TwoWay" />
You can also add UpdateSourceTrigger=PropertyChanged to make the List update on every key stroke.
Edit2:
To acces the objects gcreated in code behind you have to set tht DataContext of your WPF. See wpf xaml binding to object created in code behind

Textblock text selection

I have a chat window where each message is a TextBlock. I want to be able to select the text inside my TextBlocks. Google says to use a TextBox instead, which I cannot do because they do not support runs, which I am using to create hyperlinks inside my messages. What options do I have?
Check out the RichTextBox implementation for WPF.
How about making an CustomContentControl with TextBox (with selection style) and a Link in it and play with visibility based on content. Some thing like
<myControls:CustomContentControl>
<Grid>
// play with visibilty depending on content.
<CustomLink/>
<TextBox Style="{StaticResource SelectionStyle}"/>
</Grid>
</myControls:ContentControl>

add row note to a particular cell in datagridview

I want to add a comment to a particular cell of datagridview as the image below. Can it be done?
You can add a RowDetailsTemplate with a TextBlock in it and databind its Text property.
<sdk:DataGrid ItemsSource={Binding Items}>
<!-- Put your columns here -->
<sdk:DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Note}" />
</DataTemplate>
</sdk:DataGrid.RowDetailsTemplate>
</sdk:DataGrid>
Hope this helps.
I dount that is possible to add text over multiple cells in a row (in dgv control). Especilly if you want to keep the current text in cells, and add an extra text over these cells (like this comment).
So my answer s no, its not possible. But on the other hand, everything is possisble. You could create a new Label control, and position it on the right place, and add text into it. This means that the label would be actualy placed over the dgv control. This is one of thr idea.
I think if you wanna do something like you want to, you would have to do something extra (something unusual).
After quite some time I discovered the exact solution to prove that Matt is right.
Following is a beautiful link to do the same.
http://msdn.microsoft.com/en-us/library/85kxk29c.aspx

List box in c# windows phone 7

Does list box allow placing a paragraph of word into it instead of a item one after another?
I think you just need to put a text block inside a scroll viewer, but this article shows how to make a control:
See this article
This is some code that does the basic work:
<ScrollViewer x:Name="MyScroller">
<TextBlock x:Name="MyTextBlock" TextWrapping="Wrap" Text="Some very long text here..." />
</ScrollViewer>
You'll see that if the area containing the text is bigger than the size of the textblock, you'll be able to scroll to view all text.

Categories