I feel like I'm going to reinvent wheel so I would like to know if WPF has bult-in support for what I'm trying to achieve. I'm building an app that will allow people to enter some text in a textbox, and then see it formatted in a textblock.
I would like that the user be able to format the text himself by inputing things such as
This [BusinessSpecificStyle] is [/BusinessSpecificStyle] a sample text
My purpose is to be able to easily change the presentation of all my documents by simply changing the underlaying rules in BusinessSpecificStyle. However I don't know what is the best way to implement that with WPF. I was thinking of using a BBCode parser like this one but supposing I go that way, I don't see how I will be able to convert the resulting XAML into TextBlock children programatically, and I seriously wonder if there isn't some kind of built in support for that.
Thanks for your help
IValueConverter is what you are looking for.
Create the converter and format your text based on the bindings passed from the XAML.
You can get multiple samples over the net for creating IValueConverter. Refer to the link here and here to get you started.
Not sure if you are asking for Converter here. To me it reads that you want to control the style of a block of text depending on some background and common style?
If that is the case, you want to set the inlines of your text block to seperate your text into run elements, which can reference a specific style resource.
<TextBlock>
<TextBlock.Inlines>
<Run>This</Run>
<Run Foreground="{StaticResource BusinessSpecificStyleForeground}">is</Run>
<Run>a sample text</Run>
...
in this case, you create a resource which defines the binding styles for run or bind the Style in it's entirety.
Apologies if I am making up a new question, I see you've marked an answer but wanted to add this just in case.
Related
I'm currently working on an UWP app which also deals with math. Therefore i parse formulae and display them as inlines in a RichTextBlock. As you can imagine, I also want to display sub- and superscription - but I haven't yet found a way to do so. I know, there is something called BaselineAlignment in WPF, but it seems that this is not supported in UWP.
I'm currently also about to figure out how to best display fraction lines and roots. I've thought about building my own control for this purpose, but if somebody knows a better approach it would be nice to know.
I'm gladful for any answer.
To apply subscript and superscript to text in RichTextBlock, we can take advantage of Typography.Variants attached property. For example:
<RichTextBlock FontSize="36">
<Paragraph FontFamily="Palatino Linotype">
2<Run Typography.Variants="Superscript">3</Run>
14<Run Typography.Variants="Superscript">th</Run>
</Paragraph>
<Paragraph FontFamily="Palatino Linotype">
H<Run Typography.Variants="Subscript">2</Run>O
Footnote<Run Typography.Variants="Subscript">4</Run>
</Paragraph>
</RichTextBlock>
And the output would like:
You could use a WebBrowser control, and just locally include one of many javascript libraries for rendering LaTex.
This may seem familiar to you but I have a problem creating a custom List Box with custom style as well. I can only do one of them at the same time... I can't use both. and another thing is that could you tell me how to add the custom listboxitem in c# code so I could easily add it to the program?
what I mean is that for example if you put an image before the text of listboxitem and then give it an style, but all thease must be done for all items.
thanks to all
First, what have you already tried out? Are you expecting SO users do the work for you?
Listbox tag has a ListboxItemTemplate. For that you can implement a DataTemplate has a StaticResource and put whatever you want inside.
Check the documentation on MSDN.
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate(v=vs.110).aspx
Is there any way to replace one control with another?
I have a library from a vendor that uses a TextBox and I would like to change it to a RichTextBox.
Can I maybe set up a Style with a TargetType=TextBox and assign it to RichTextBox?
I do have access to the vendor code, but putting the replacement in the parents Resources would be much easier than updating their code each time they have a new release.
Is this even possible?
Thanks!
Without knowing more about the control(s) in question, I would recommend using the "Edit Template > Edit a copy" in VS or Blend and changing the TextBox to a RichTextBox. This should leave the rest of the template intact.
If that doesn't work, please post the ControlTemplate and/or Style code you used, and maybe a screenshot of the vendor control.
In my current project i'm trying to show a label which has a TimeSpan binding.
Actually the xaml looks simple:
<Label Content="{Binding ElementName=Root, Path=DataSource.TimeTotal, StringFormat={}{0:hh\:mm}}"/>
Which is exactly what is shown in this question. But it doesn't work, it shows always the full time and not only hours + minutes.
I tested a lot of solutions but none of them worked.
Suggested solution
Maybe just try to use TextBox instead of a Label and bind Text instead of Content. You can find example of the binding here: How to format TimeSpan in XAML. It's also worth noticing that TextBlock is more lightweight then Label, so it should be used when possible.
Alternative solutions
If you want to use Label for some reason, there seems to be a property called ContentStringFormat of a Label itself, so your xaml can look like:
<Label Content="{Binding ElementName=Root, Path=DataSource.TimeTotal}" ContentStringFormat="hh\\:mm"/>
Another solution would be implementing a custom IValueConverter, where you can pass string format as converter's parameter and format the value manually in the converter's code. This solution should then work correctly for all appropriate control types. More about value converters can be found here and here.
String formatting properties
As Erode wrote in the comment, there seem to be other controls for which StringFormat does not work.
However, for controls derived from ContentControl you should be able to use ContentStringFormat and for controls derived from ItemsControl, there is a ItemStringFormat property. There is a link with few samples presenting how to use those:
http://blogs.msdn.com/b/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx
It works with TextBlock, which is the lightweight equivalent to Label. Whenever you're binding to a dependency property in a control that uses a ContentPresenter to display the data, it seems to eat the StringFormat you've provided. (in Label, Button, etc). Wish someone would let us know why! You can nest a TextBlock within the Label or just go with a TextBlock (which, unless you need access text, is usually a better choice anyway).
I'm trying to style some wpf user controls to make them look the same i have in my web application. I found some really great example for every control i need except for sliders and radio buttons.
Here is how they should look like:
Slider:
Radio Button:
I know i should create some ControlTemplate but i don't really know where to start...
If you can provide a complete example it would be perfect, but i guess that even some good deep advise would fit my needs.
Thank you 1000!
I have style sample here. It renders this:
I would start with Control Styles and Templates on MSDN pages. There are examples for most controls which give a good overview of how the control template works. Also you can extract/download the original control template and try to make your changes to it. Download default control templates
P.S. Actually the slider example on the first link resembles the one you describe.
Try using Expression Blend.
Learn Expression
and specifically this video: Creating ControlTemplates.
Expression Blend is something like a XAML design application - you can size, colour, adjust fill and stroke etc on all the elements in a control. It takes a little while to get used to, but you will be able to design controls with a graphical UI and Expression will provide you with the appropriate XAML to use in your project.