I am new to silverlight development and PRISM framework. We are using http://happynet.codeplex.com/ as a template. The localization is working well if resource text is bind directly in XAML. But it is not working well for :
Texts in ViewModel Classes.
Grid Header Column Texts in View's Code behind.
They requires reloading after language switch. How can it be done so that text change occurs on language switch?
I don't know much about Happynet as it is the first time I see the project.
It depends how they implement localization but if you want dynamic localization (eg. selecting a language in a combobox) it can be done. Follow the following steps to set it up:
http://vanderbiest.org/blog/2009/11/30/silverlight-resource-files-localization-language-specification/
To dynamically change it, you just need to set the current UI thread.
static void SetLanguage(string culture)
{
if (culture != System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.CultureName)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
}
}
We can make WPF Custom Control for overriding some control's look&feel, like icon button.
public class MyButton : Button {
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
}
...
}
But I think that way has a some problem. It causes a some issue when I distribute the Custom Cotrol 'MyButton'. Because MyButton dependent on external resource, the WPF Style MyButton. So I need to distribute two files : MyButton.cs and MyButton.WPF.
So, How can I definite a Conotrol Template by programmatically?
(Of cause, another way to solving the problem is making WPF User Control. but my point is not that.)
Note : I found some resources about this issue. That was a Inline XAML scripting. But to me, the XAML scripting is not option. Because I'm learning on WPF so I want to know WPF thatself, not a trick.
You do not distribute the code, you distribute a dll that contains the class and the generic.xaml
That way another designer/developer can 'override' the template and your template stays as a safe fall-back.
EDIT
Defining a Template in code is no fun (a lot of code, hard to debug, hard to maintain) but it can be done:
var template = new ControlTemplate(typeof(MyControl));
EDIT2
Another hack is to specify the template in a long string and use the XAML Parser to load it.
I have read in two different books that in WPF, the ToolBar.Header property doesn't do anything:
Windows Presentation Foundation Unleashed by Adam Nathan, pg. 119
Pro WPF with VB 2008, pg. 650
However, I'm creating my ToolBar objects dynamically like this (tbtToolBar is actually a ToolBarTray defined in the Xaml, vm is the window's ViewModel):
foreach (IToolBarViewModel toolBarViewModel in vm.ToolBars)
{
ToolBar toolBar = new ToolBar();
toolBar.DataContext = toolBarViewModel;
// Bind the Header Property
Binding headerBinding = new Binding("Header");
toolBar.SetBinding(ToolBar.HeaderProperty, headerBinding);
// Bind the Items Property
Binding itemsBinding = new Binding("Items");
toolBar.SetBinding(ToolBar.ItemsSourceProperty, itemsBinding);
tbtToolBar.ToolBars.Add(toolBar);
}
And the Header property clearly shows up in a label as the first item in the toolbar. This is not the behavior I want. I would like to use the Header as a title in a drop-down list of ToolBars when the user right clicks on the ToolBarTray, just like the books describe.
So, I tried to get rid of the Header by setting:
toolBar.HeaderTemplate = new DataTemplate();
This works, but now there's a small unsightly gap in the toolbar.
Is there a way to make the header invisible without the gap?
Why are the books clearly wrong? Did something change between then and now?
The only way I was able to make this work was to keep the Header property null, and create another property on the ToolBarViewModel called Name.
What would be a good approach to display and edit large amount of unformatted text (just like notepade does) using WPF? Loading a big string into a TextBox makes the UI unresponsive. The overall performance is not nearly comparable with TextBox Controls of previous Microsoft UI Frameworks.
What options do I have to solve this problem. I do not want to block the UI thread while the text control loads the text. Also I might need some sort of "virtualization" because it might not be a good idea to load the whole text into the control (I guess that 20MB of text would create a lot of glyphs even if they are not visible). It seems that TextBox doesn't even have an AppenText() Method anymore so I don't even have a way to control asynchronous loading of the text.
Isn't this a common problem? It seems that WPF does not provide anything for this out of the box. Why is this so?
AvalonEdit, the text editor in SharpDevelop, was written completely from scratch in WPF and is optimized for large amounts of text. It doesn't support rich text (although it does support syntax highlighting and other cool features such as folding). I think this might fit your bill perfectly.
Here is an article on the editor written by the developer:
http://www.codeproject.com/KB/edit/AvalonEdit.aspx
I am not sure if this helps, but have you tried using FlowDocumentPageViewer and FlowDocumentReader?
It also has very good annotations support and looks ideal for loading documents in text format.
The problem is that the TextBox is a single container element. List controls, such as ListBox virtualize very well because of container recycling. There really isn't anything simple that you can do to speed up the TextBox.
But the TextBox control does have an AppendText() method:
TextBox tb = new TextBox();
tb.AppendText("Hello");
So yes, you can use this to dynamicly add some text just like you mentioned.
You can just use a textbox with a style that gives the user more room to view the text. There are probably more advanced controls from Telerik and others but if you don't require editing options that should suffice.
You could always mix and match technologies: you could drop a WinForms TextBox onto a WPF parent. You lose things like styling, opacity, animation, transforms, etc., but if all that matters is editing text, the WinForms TextBox does that just fine.
Have you tried the WPF RichTextBox? You'll definitely want to read up on the FlowDocument information if you go this route.
You could use FlowDocument, but this doesn't work out of the box to bind to the Document property of a FlowDocument in MVVM.
Another solution is using FlowDocumentScrollViewer and bind to its Document property.
(or you could even use a FlowDocumentReader and bind its Document property, similar to the FlowDocumentScrollViewer. This gives you a different UI.)
The View:
<FlowDocumentScrollViewer Document="{Binding FlowDocument, Mode=OneWay}" />
The ViewModel:
FlowDocument fd = new FlowDocument();
Paragraph p = new Paragraph();
Run r = new Run();
r.Text = "large text";
p.Inlines.Add(r);
fd.Blocks.Add(p);
FlowDocument = fd;
private FlowDocument _FlowDocument;
public FlowDocument FlowDocument
{
get{ return _FlowDocument; }
set
{
_FlowDocument = value;
NotifyOfPropertyChange(nameof(FlowDocument));
}
}
see also this for extra performance tips: https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/optimizing-performance-text#flowdocument-textblock-and-label-controls
How about trying something like this:
Keep the whole string in memory but show only a 'slice' of it in the textbox. Size of the that sliced string would be dynamically calculated depending on the size of textbox, font size etc.
Of course this involves a lot of not trivial code for proper displaying, synchronizing and so on, but it seems the way to go.
One other option is to use Scintilla.NET. It has the best performance for large amounts of texts I have seen so far. Loading large files is almost instantaneous. Even though it is a WinForms control, you can embed it into your WPF window with WindowsFormsHost container. There is syntax highlighting and some other features that should be more than enough for displaying unformatted text. From the downsides - since this is a WinForms control, it overlaps other WPF UI elements. Also there may be issues with resizing the control. Unfortunately, I don't see any better option than using a non-WPF control instead of the default TextBox and RichTextBox.
I am trying to send my dynamically created silverlight 2 page/image to a an ASP.net web service to render it as an bitmap image.
I can see many examples how to get the XAML using javascript (see here) in version 1 of silverlight but I have a few differences.
a) I am using silverlight 2 RC1
b) I have dynamically add controls to the page in c#
c) I would prefer to use c# to get the new XAML to keep all the coed in one place.
does anyone know how to extract the XAML from a control or the page in c#?
Thank you
Update: This is now possible under silverlight 3 using a writable bitmap to save the XAML as a JPEG see my blog post here: http://blog.blueboxes.co.uk/2009/07/21/rendering-xaml-to-a-jpeg-using-silverlight-3/
unfortunately there is not a method for a .ToXaml() on an element tree unfortunately. You can use VisualTreeHelper to build such a method and recurse through a particular element I suppose.
SilverlightContrib has a XamlWriter class that can extract the XAML from a live control.
It's free.
The link is: http://silverlightcontrib.org
The code would be something like:
// using SilverlightContrib.Xaml;
var cb = new GroupBox();
StringBuilder sb = new StringBuilder();
XamlWriterSettings settings = new XamlWriterSettings();
using (XamlWriter writer = XamlWriter.CreateWriter(sb, false, settings))
{
writer.WriteElement(cb);
}
string result = sb.ToString();