Get Text From RichEditBox - c#

I have a RichEditBox where the user can write their own text, like below:
<RichEditBox
x:Name="jawabBox"
Grid.Row="0"
FontSize="21"
FontWeight="SemiBold"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="#FFDBDBDB"
Foreground="Black"
CornerRadius="15,15,15,15" />
How to get the text that has been written by the user? Or how can the user write text into a textbox with multiple lines, other than using RichEditBox?

As #Flydog57 mentioned, you could get the text via the ITextDocument.GetText() Method. It requires a TextGetOptions Enum as parameter and a string as output value.
You could use it like this:
string value = string.Empty;
jawabBox.Document.GetText(Windows.UI.Text.TextGetOptions.AdjustCrlf, out value);

Related

How to use a font unicode that is stored in c#

I have a list of objects called SidebarItems, this list may change according to what Module the user is in. I have downloaded and got Font Awesome Pro working in my application, However I must use the Unacode to access the correct icon inside the font.
The SidebarNavItem
public record SidebarNavItem(string Title, string ViewName, string IconUnicode);
public class SidebarItems:IReadOnlyCollection<SidebarNavItem>
{
//Left this out for brevity
}
The Xaml That its being used
<Button Style="{StaticResource LabeledIconButton}"
Content="{Binding IconUnicode}" Grid.Row="0"
behaviors:ButtonBehavior.Label="{Binding Title}"
Command="{Binding NavigateCommand}"
CommandParameter="{Binding ViewName}"/>
This is what I'm getting but when I type it into the button, I get this:
<Button Style="{StaticResource LabeledIconButton}"
Content="" Grid.Row="0"
behaviors:ButtonBehavior.Label="{Binding Title}"
Command="{Binding NavigateCommand}"
CommandParameter="{Binding ViewName}"/>
How can I store these codes in SidebarItems?
Replace &#x with \u and remove the trailing ; from the string value returned from the property.
So  becomes:
public string IconUnicode => "\uf319";

Display multiline DynamicResource Label

I wish to add a Label in WPF that displays string from two different DynamicResources.
I want each DynamicResource to be on a new line.
My existing code is:
<Label x:Name="MyTextDisplay"
Grid.Row="3"
Grid.ColumnSpan="2"
Background="Red"
BorderBrush="Blue"
BorderThickness="1"
Margin="2, 2, 2, 2">
<TextBlock TextWrapping="Wrap" Text="{DynamicResource MyTextLine1}" Grid.ColumnSpan="2"/>
</Label>
I have another DynamicResource called MyTextLine2 that I want to display below MyTextLine1 but in the same Label.
How can I do this?
I have looked at these examples here but they dont display on new lines: How to bind multiple values to a single WPF TextBlock?
I faced the same problem and finally I found a solution.
Just use \r\n linebreak instead of just \n.
So, your resourse must look like:
<system:String x:Key="MyText" xml:space="preserve">Line 1
Line 2</system:String>
I realy don't know why this notation must be used only for dynamic resources, but it works for me

How to allow HTML tags in WPF text block with binding?

I defined a TextBlock in xaml with binding:
<TextBlock Text="{Binding ElementName=MyClass, Path=MyStringProperty}"/>
When I set MyStringProperty to , for example , <b>Hello, World!</b> it shows it as plain text. Is there a way to tell the control to make the Hello, World! string bold?
You can't do this with a TextBlock like that.
You need to bind the FontWeight property of the text block to a variable that holds the bold/not bold value.
If the value is a boolean you need to write a converter to map the boolean value to the font weight property.
If you want to change the boldness (or any other property) of the text at runtime based on what the user types in then you need to be looking at using a RichTextBox.
Use the Inlines property:
<TextBlock Text="{Binding ElementName=MyClass, Path=MyStringProperty}">
<Run FontWeight="Bold" Text="{Binding BoldText}" />
</TextBlock>

How to set TextBlock text property to a string resource?

I've been following this tutorial Using String resources (xaml) to set up string resources, in a Windows Universal project. But even though I've set the Uid of the text block to the string resource name, the string contents aren't displayed in the text block during testing the app.
Does anyone know where I might be missing a step in setting up the string resource
as the text block text value?
This is the xaml definition for the text block, showing the Uid set to the same name as the name of the string in resources "About":
<TextBlock x:Uid="About"
Grid.Row="1"
Grid.RowSpan="2"
Width="400"
Height="300"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text=""
TextWrapping="Wrap" />
This is the res file itself:
And this is the structure of the source tree:
Resources files when accessed using UID are usually of the type Controlname.Property for which you want to bind.
So it should be About.Text not About tested And Working

Save text box data to a local file in a Windows 8 app

I'm learning how to build Windows 8 apps in XAML and I'm somewhat new to it. I have three text boxes and a button on a page and I want to take the text in each text box and save it to a file locally.
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBox HorizontalAlignment="Left" Margin="321,160,0,0" TextWrapping="Wrap" Text="First Name" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Margin="321,211,0,0" TextWrapping="Wrap" Text="Last Name" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Margin="321,260,0,0" TextWrapping="Wrap" Text="Age" VerticalAlignment="Top"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="321,324,0,0" VerticalAlignment="Top"/>
</Grid>
It doesn't really matter what kind of file it is, I'm going to assume a .txt file is fairly easy to create. I've tried to wire up a FileStream on the button click, but intellisense wouldn't allow me to add the necessary usings for it. I'm starting to get the feeling that there is another way you're supposed to handle binary files in Windows 8 apps. If someone could guide me into the right documentation or quickly show me how to set it up, I'd appreciate it.
A fairly easy to use method is File.WriteAllLines as shown in this topic
All you have to do now is retrieve the value from the textboxes. Make sure you assign them a Name property, so you can target them in your code-behind:
<TextBox Name="ageTextBox" HorizontalAlignment="Left" Margin="321,260,0,0" TextWrapping="Wrap" Text="Age" VerticalAlignment="Top"/>
Then in your Code-Behind, say on the button click, add the Text to a list:
List<string> myList = new List<string>();
myList.Add(ageTextBox.Text);
Finally, you write the contents of your list to a specified file:
string path = "D:\\Temp\\MyFile.txt";
File.WriteAllLines(path, myList);
Do note you have to create the eventhandler yourself, name the other textboxes and add their texts to the list too.

Categories

Resources