Textblock text selection - c#

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>

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.

Change a section of a window upon click event

I have a treeview at the left side of the screen, and when I click on any of the TreeViewItem, I want the right side of the screen to change accordingly.
For example, clicking on 'Project' would display on the right half of the screen, a label for project name along with the project name in a text box, and a similar label-textbox pair for some other fields. Clicking on a sub-option of 'Project' such as 'Task 1' should change the right half of the screen such that instead of labels and textboxes for project name and details, it should now be for task name/details. Atm, I only care about label-textbox pairs but in the future I'll need some more sophisticated options, maybe buttons and tables.
What I thought of was to have a grid premade for each option, when I clicked on 'Project' there would be a grid which displays all the info for a Project. And when I then clicked on 'Task 1', the Project grid should be hidden and the Task grid should be displayed with the fields filled out.
Is this possible? What should I be using to create templates that I can then choose from?
Firoz already mentioned the important bit. A rough guess is that you're not using MVVM pattern, so to minimize the adaption effort, you could add a Content Control to your window and set the content of this control whenever a selection is made. You can put any User Control in there.
Using MVVM would mean you bind that Content Control to a property on your ViewModel (of type UIElement or UserControl) and set an instance whenever a bound selected values changes. Speaking of selected Value, I think the default TreeView is not really Binding-friendly, so you might end up with behaviours that do the binding for you.
What you are asking to do is quite easy and possible, but I don't think you are thinking quite big enough.
As your project grows and the number of different things that you want to show expands, then you are going to need to show and hide more and more controls. This is quite quickly going to get unmanageable. Instead think about some other controls deal with this, in some ways you are doing something very like a tabbed dialog, just with a hierarchical set of tabs.
A tabbed dialog has a panel and a set of tabs, when you click on each tab, the content of the panel changes. In fact you can create UserControls one for each specialised set of UI that you want to display, e.g. you could have a ProjectControl that displays all of your project textboxes, labels, buttons etc.
In addition WPF has this neat feature called DataTemplates, these define how a type of data should look when it is displayed. So if you where to have a
public class MyProject
{
public string Name {get;set;}
}
Then you could define
<DataTemplate DataType="{x:Type MyProject}>
<TextBox Text="{Binding Name}"/>
</DataTemplate>
And WPF will automatically convert the data into to its visual form if you set it as the content of the tab panel.
However this type of displaying content in a panel is not the only WPF control that does this. There is also something called a NavigationFrame, which also can be used wrapped into a Window as a NavigationWindow. This control provides you ways to navigate to the next Page to display. Pages can be just like the UserControls in a tabbed dialog, but can also be URIs, enabling you to link in content from the web if you wish. In addition you can call NavigateTo from other controls enabling you build much more usable interfaces.
I worked through the process of building a full windows control panel style interface in
http://alski.net/post/2012/01/11/WPF-Wizards.aspx
and http://alski.net/post/2012/01/13/WPF-Wizards-part-2-Glass.aspx
I've added later VS2012 style glows in
http://alski.net/post/2013/09/14/WPF-Re-creating-VS2012Office-2013-window-glow.aspx
And then released the entire source code as open source at
http://winchrome.codeplex.com/
This comes with support for embedding Navigation panels with
<WinChrome:SearchableNavigationWindow
x:Class="WinChrome.Win7Demo.MainWindow"
...
xmlns:WinChrome="clr-namespace:WinChrome;assembly=WinChrome"
Style="{StaticResource Win7NavigationWindow}">
<WinChrome:SearchableNavigationWindow.Navigation>
<view:Navigation x:Name="navigationTree"/>
</WinChrome:SearchableNavigationWindow.Navigation>
(Full source code)
Where the navigation window is embedded as, but can also be a TreeView.
<UserControl x:Class="WinChrome.View.Navigation" ...>
<ScrollViewer HorizontalScrollBarVisibility="Disabled" Padding="12,0"
VerticalScrollBarVisibility="Auto" >
<StackPanel>
<Button
Margin="0,12,0,0" Style="{StaticResource LinkNavigatorButtonStyle}"
Content="Home"
Command="{Binding
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Win7Demo:MainWindow}, AncestorLevel=1},
Path=GoHomeCommand}" />
</StackPanel>
</ScrollViewer>
(Full source code)

Metro application directly focuses on my first field

I'm creating this test Metro application using Windows 8, VS2012, C# and XAML. There are different TextBox in the application page arranged in a StackPanel. When the application is launched the focus is on the first TextBox.
I was wondering how to "deactivate" this.
Here's a pic, as you can see the first field is focused (color changed and ToolTip displayed).
When your UI is loaded you can remove focus from the TextBox by applying a Programmatic focus state to any other control.
Imagine that you have a Button named myButton. You can:
myButton.Focus(FocusState.Programmatic);
You cannot however use FocusState.Unfocused state to remove focus from the TextBlock because it is not allowed and will throw an exception.
One simple fix for this is place something to catch it first with IsTabStop="True" with a 0 Opacity which is a bit hacky but the only way I know. So something like;
<TextBox IsTabStop="True" Opacity="0" Height="1" Width="1"/>
<!-- Then the rest of your content like your other TextBox stuff -->

How do I make clickable text

I have been trying to create buttons on Windows Phone 8, which don't look like buttons, but just look like text, however, I haven't been able to find this question answered anywhere else. I am trying to create something like what Microsoft have used below for the camera roll, albums and date buttons below. Is anybody able to explain how I can do this, or maybe link me to a tutorial or something that I may have missed while searching? Thank you.
Windows phone uses XAML code to create UIElements. Very similar to WPF, you can use almost any UIElement as a button. This is because each element has a large amount of events that can be tracked. Think of it as a layered cake. If you have a textblock inside of a listbox inside of a grid, similar to what you see above. Then when someone clicks on the textblock it will try to handle the event. If it isn't set to handle it then the listbox tries. If the listbox cant then the grid tries and so on. What you are looking for is the tap event in the textblock. Google textblock tap event.
<Grid x:Name="LayoutRoot" Background="Transparent">
<ListBox>
<StackPanel>
<TextBlock Tap="title_Tap_1" Name="title">title</TextBlock>
private void title_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
//Your code here
}
I tend to use a ListBoxItem to wrap a TextBlock. It allows you to use the TiltEffect from the wpToolkit to show interaction and also exposes a Tap event for the ListBoxItem
<ListBoxItem toolkit:TiltEffect.IsTiltEnabled="True" Tap="On_Tap">
<TextBlock>Hello World</TextBlock>
</ListBoxItem>
I don't know for Windows Phone 8 because I haven't written any app yet. I think that this is like Windows Phone 7 , 7.1 that I used to write code. This control that you see is a ListBox and inside there are ListBoxItems. ListBoxItem can be anything (it can be used as a container to insert anything inside it.). Hope it helps.
The easiest way is to use HyperlinkButton instead of classic Button. In that print screen I doubt there's a list for only like... 3 buttons (hyperlinkbuttons).
Controls in the xaml world are look-less: use a button in your ItemTemplate and turn the border off. Then you can use the command property of the button for binding to your VM, or if you are not using MVVM use the Click event of the button and handle it in the code behind.

Change background colour of TextBlock in C#

Currently porting an application to Windows Phone 7 I've encountered a problem that should be trivial
All I want is change the background colour of a TextBlock.
Using the WYSIWYG I can easily create a TextBlock, change the foreground and background colour.
So for a TextBlock using white text on black background I would use:
<TextBox Height="148" HorizontalAlignment="Left" Margin="106,0,0,0" Name="textBox1" Text="TextBox" VerticalAlignment="Top" Width="460" Background="Black" BorderBrush="Black" Foreground="White" />
But I need to do it in code (C#) and the Background doesn't appear to be a property of TextBlock.
How come it's something you can do using the resource editor, but not in code?
I've found various similar questions, but no definitive answer.
In Microsoft documentation (.Net), TextBlock does appear to have a Background property
Is there a way to do this in code without having to put the TextBlock inside a container (like Grid) which does have the Background property?
Thanks
JY
TextBlock is not inherited from Control, it doesn't have a Background property. The code you are showing is a TextBox not a TextBlock. TextBox inherites from Control and has a Background property. The simplest way is to wrap it with a Panel, or you can create a custom control for it.
Also, in silverilght sdk, you have a control called Label and it is inherited from Control. You can probably get the source code from there and implement it in your project.

Categories