Getting text color from Silverlight RichTextBox - silverlight

I have a problem with the getting the RichTextBox`s text color. There is a method about it but i was fail with it. help me to use this method or is there any way to do this.
Here is the method
run.Foreground(DependencyProperty)

I'm not sure what that method is supposed to do, but in XAML you would use:
<RichTextBox Name="MyBox" Text="Some text" Foreground="Red" />
or you could bind it to a Brush property:
<RichTextBox Name="MyBox" Text="Some text" Foreground="{Binding myColor}" />
If you're trying to set it from code behind:
Brush myBrush = new SolidColorBrush(Color.Red);
MyBox.Foreground = myBrush;

I just find the answer of my question
((System.Windows.Media.SolidColorBrush)(run.Foreground)).Color.ToString()
with above code i can get the color of the text in hex.

Related

WPF show small number beside all Controls

I have many FrameworkElements (TextBlock, CheckBox, ListBox..) and I would like to make something allowing me to show a small number besides every one control.
Some text ³
I came with the idea to write a MarkupExtension, where I could write that number like this:
..
<TextBlock Text="Some Text" SomeExtension="3" />
..
and then to add it somehow to the template of the Control.
But I'm sure, you guys have better solution for this problem ;)
One way to go with it would be create a Attached Property. Upon setting it on a control, a custom Adorner would be added for that control showing specified number.
Use the tag property to provide the number you want and inside the custom template databind to the property
<TextBlock Text="Some Text" Tag="3" />
and inside the controltemplate
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}"/>

XAML text binding

I would bind a string property to text property like this: Text="{Binding propertyName}.
I also want to append a hardcoded string to this like Text="{Binding propertyName} appendedName. How to do this?
Text="{Binding propertyName,StringFormat='Your property is: {}{0}'}"
You could use Run Text:
<TextBlock>
<Run Text="{Binding YourBinding}"/>
<Run Text="Suffix"/>
</TextBlock>
If you want to use it like this several times I would recommend a TemplatedControl where you have a Suffix DependencyProperty and a Text DependencyProperty.
You should create new property that returns text + appendedName.
Another way is to use several text blocks.

Silverlight 4 RichTextBox Bind Data using DataContext

I am working with Silverlight 4 and trying to put my test apps multilingual but I am having some trouble when I arrive to the "RichTextBox" control. I am able to bind it properly by doing back-code (c#), but when trying using the "DataContext" attributes I am not able to load it at all.
I have created a FormatConverter that return a Block (paragraph) for testing and my code where I have my RichTextBox looks like:
<RichTextBox x:Name="rtaTest" BorderThickness="0" IsReadOnly="True" UseLayoutRounding="True"
DataContext="{Binding Source={StaticResource Localization}, Path=Home.MainContent, Converter={StaticResource ParagraphFormatConverter}}">
</RichTextBox>
I am wondering if there is a way of binding a RichTextBox from the XAML.
Run seems to support databinding in SL4, as in:
<RichTextBox>
<Paragraph>
<Run Text="{Binding Path=LineFormatted}" />
</Paragraph>
</RichTextBox>
I think you may be a little confused about the used of the DataContext. You might for example have some Rich text where some children of one or more InlineUIContainer elements may retrieve their text from a property of some object. You would assign the object to the DataContext.
Whilst I'm not quite sure what you were expecting to achieve but I suspect that what you really need is for your converter to actually return a BlocksCollection (even if it just contains the single Block you were originaly returning) and then to bind as:-
<RichTextArea x:Name="rtaTest" BorderThickness="0" IsReadOnly="True"
UseLayoutRounding="True"
Blocks="{Binding Source={StaticResource Localization},
Path=Home.MainContent, Converter={StaticResource ParagraphFormatConverter}}" />
This FillFromXml is a WPF thing? Don't see it in Silverlight.
Blocks cannot be set, they can only be fetched. One way to set the blocks for a RichTextArea is
public static void UpdateRichTextArea(RichTextArea area, string xmlText)
{
if (area == null)
return;
area.Blocks.FillFromXml(xmlText, true);
}

WPF: How to embed button inside text flow (wrap text around button)?

I'd like an advice to the following problem: I want to embed a Button into a text flow, but when I embed a Button and Label (or TextBlock) into the WrapPanel, I get the first figure:
alt text http://sklad.tomaskafka.com/files/wpf-wrappanel-problem.png
I think that one of solutions could be FlowDocument, but I feel that this is far too heavy for a control simple like this (which could be used in several hundred instances). Do you have some other ideas about how to implement this? Thank you!
EDIT:
One solution could be the following (I didn't know it was possible to put more stuff into TextBlock), but I would lose the ability to bind (which I need):
<TextBlock TextWrapping="Wrap">
<Span>
<Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
<Run x:Name="MyLabel" Text="{Binding Path=Subject}" />
<!--
Problem: binding makes following error:
A 'Binding' cannot be set on the 'Text' property of type 'Run'.
A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
-->
</Span>
</TextBlock>
To bind to Run.Text, checkout the BindableRun class by Fortes. Simple to implement, I use it all over my projects.
I found that implementing BindableRun correctly is pretty tricky - and almost all other available implementations will cause an exception from wpf layouting engine when the bound content changes from null to something non-null - see this problem, keyword "Collection was modified; enumeration operation may not execute."
Corrrect implementation from Microsoft is here - it shows how tricky this really is.
Solution: BindableRun class + the following markup:
<TextBlock>
<Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
<common:BindableRun x:Name="Subject" BindableText="{Binding Path=Subject}"/>
</TextBlock>
Funny thing it works on the designer of a UserControl...
In that case, using the Property Change of your control to set the value to the Run is enough. I mean, if you had something like:
<TextBlock>
<Run Text="{Binding ElementName=thisCtrl, Path=Description}" />
</TextBlock>
Then just name the run, and on your property change handler of your UserControl DependencyProperty get/set the value.

WPF Textblock, linebreak in Text attribute

Is there a way to have \n make a line break in a TextBlock?
<TextBlock Text="line1\nLine2" />
Or is there a better way to force a middle line break, inside the Text attribute?
<LineBreak />
This doesn't work for me, it needs to be the value of the Text attribute, because the text string is being set from an outside source.
I'm familiar with LineBreak but it's not the answer I'm looking for.
Try this:
<TextBlock>
line1
<LineBreak />
line2
</TextBlock>
I know this is ressurecting an old question, but I had the same problem. The solution for me was to use HTML encoded line feeds (&#10;).
Line1&#10;Line2
Looks like
Line1
Line2
For more of the HTML encoded characters check out w3schools
The easiest way is
<TextBlock> blabla <LineBreak /> coucou <LineBreak /> coucou 2 </TextBlock>
So you just write XAML code, and the <LineBreak /> has exactly the same meaning the in HTML or the "\n" in C#.
<LineBreak/>
http://www.longhorncorner.com/UploadFile/mahesh/XamlLineBreak06092005152257PM/XamlLineBreak.aspx
How about breaking the line into two tags?
<StackPanel>
<TextBlock Text="Line1" />
<TextBlock Text="Line2" />
</StackPanel>
<LineBreak/> will not work if it is inside a collection such as Grid or StackPanel.
In such cases the following would work as shown:
Correct way to use it may be the following :
<TextBlock>
<Span>text1</Span>
<LineBreak/>
<Span>text2</Span>
</TextBlock>
The Best way that worked for me for multiple lines in the same Textblock is:
<TextBlock>
text1
<LineBreak/>
text2
</TextBlock>
Make sure to not use TextWrapping="Wrap". Use TextWrapping="NoWrap" or use nothing.
<HyperlinkButton
Content="Apply and restart this pplication!
Note that modifying these settings requires the application to be restarted." />
CRLF simple way = !
!
- Work on all wpf, xaml, silverlight controls like TextBlock, HyperlinkText and more
If you are binding TextBlock's Text, none of the other answers work. Simply add '\n' to the binding text to where you want to break.
this &#10; did not work for me, when I used binding. But this works:
$"first line {Environment.NewLine} second line"
I'm late to the party but ..
this is more or less how I did it ,(mind my ItemSources are plain strings, not formatted , and I didn't need to 'convertBack' anything)
public class SpaceToLineBreakConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (!String.IsNullOrEmpty(value as string))
? new Regex(#"\s").Replace(value as string, "\n")
: value;
}
public object ConvertBack(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
This also works fine:
<TextBlock>
<Run Text="My nice text"/>
<LineBreak/>
<LineBreak/>
<Run Text="After some linebreaks, I'm back!"/>
</TextBlock>
I was having a similar problem and wanted to bind a String of xaml markup to a TextBlock. Essentialy storing the declarative markup inside a TextBlock in a string for later use.
This is how I did: I subclassed the TextBlock to make the InlineCollection bindable and wrote a Converter between the string and an InlineCollection(or actually a generic list of Inlines.)
just use the AccessText control. you can use it like a label and you have the property TextWrapping="WrapWithOverflow"
eg.
Mine is like that and it's working fine. Also, you don't have any problems on changing the text dinamically.
This also works fine.
Using this method we can modify the text properties in each line as we required.
<TextBlock>
<StackPanel>
<TextBlock FontSize="12" FontWeight="Bold" >My Text One</TextBlock>
<TextBlock FontFamily="Times New Roman" FontStyle="Italic">
- My Text Two
</TextBlock>
</StackPanel>
</TextBlock>

Resources