WPF xaml Visual Studio keeps automatically changing pathway to my resource - c#

Having issues using a custom font in a WPF app.
To use the font, I set FontFamily="pack://application:,,,/Resources/#iconfont" on my label. However, whenever I go to edit any of the properties of the label in xaml, Visual Studio is automatically changing the font family pathway to FontFamily="iconfont", and the custom font no longer works. Any way to fix this? A setting I need to change? It's infuriating!
Edit: As long as I'm switching between properties of labels, FontFamily stays the same. But if I go to edit the properties of another type of control, and then come back to the label, FontFamily resets to "iconfont".

For starters let's make life easier and get your clutter cleaner by taking that long pack string and making it a defined resource in your resource dict like app.xaml, window.resources, wherever we can hit it from wherever we need.
So instead of putting FontFamily="pack://application:,,,/Resources/#iconfont" on every single instance it's used (which can be a performance hit by the way) we do;
<FontFamily x:Key="IconFont">
pack://application:,,,/Resources/#iconfont
</FontFamily>
We take that and plop it in your resource dictionary or wherever you want so it's referenced and loaded once from one place and made available to wherever else. Then at your instance you just do;
<Label FontFamily="{StaticResource IconFont}"/>
Now your editor should not only stop screwing with you but you have a cleaner way of maintaining that sucker. I mean imagine if you had to change your path string, or you decide to use a different font pack, would you rather do it on every instance, or in one spot that inherits to everywhere it's used?
Hope this helps, cheers.

Related

Clean "settings" binding in WPF

I was recently working with a lot of bindings to my configuration settings in XAML. Storing column widths/control sizes/window positions and the like. So I was wondering if there was an easy way to create and bind to "settings/configuration" values to XAML?
Right now I just create a setting in the project, shove a bindable property into the XAML's DataContext and go from there. But my settings count is getting pretty crazy and managing them is getting painful (boring, repetitive, and annoying).
In an ideal world I'd like a system where I can do something like this:
<Window State={Binding {Settings Name="MyWindowState", DefaultValue="Normal"}}/>
If the "MyWindowState" setting doesn't exist, it would be created automatically and stored somewhere. And if the MyWindowState setting changes, all the bindings that use it would also be notified and updated accordingly. And the DefaultValue would be used if the setting retrieval failed.
Does something akin to this exist already, or can it be achieved with the standard WPF XAML?
I am planning on working on something that can do this, but if a proven solution already exists I would love to at least look at it/hear it out.
From what I understand Telerik's persistance framework can do something like this, except on a control to control basis (there is no global "settings" I can bind to), at least on first glance.
Yes, it's quite possible. If you have an application properties file, you can access it like this:
Height="{Binding MainWindowHeight, Mode=TwoWay, Source={x:Static p:Settings.Default}}"
where MainWindowHeight is a setting (in my case, an int). You'll also need to include this in the top of your XAML file, in the Window or UserControl tag:
xmlns:p="clr-namespace:APPLICATION_NAME.Properties"
where APPLICATION_NAME is the name of your application.
EDIT: The binding can be have any mode, I just use TwoWay so I don't have to have any actual code to update it. For the positioning of my windows, it works out nicely that way.
EDIT: Also, this can't dynamically create settings. I would use an XML file in your application, make a class to handle that, and then bind to a method of the class to get/dynamically create the values.
You can do this with an attached property:
<Window loc:WindowState.Name="MyWindowState" />
In the OnNameChanged event handler of your attached property you will have access to the Window instance that the WindowState.Name property was set on and access to the value ("MyWindowState" in this example) that was set. There you start listening (e.g. using PropertyChangedEventManager) to changes of all properties of your Window instance that are part of your window state that you want to persist.
May be you can use WPF's theming option. You can store settings of your controls (width, color...) in a theme file, which is nothing but an xml file. You may store this xml somewhere and load it at runtime. You can update this xml when the application exits with the changes. And load it when the application opens next.

Change label text based upon CurrentUICulture

I've recently been tasked with localizing a small piece of software that my company created. I've been able to create the resx file for the particular culture, and things are well and good. Thing is, that there are roughly 50 or so labels/buttons/etc, that need to be changed. My question is, do I need to write in the code to set the text, or is there an easier way? For instance,
lblText.text = Resources.labelText
for every single button/label. Or is there a property in the button/label that I'm missing that would set it. Thanks.
You can change the text directly with the form designer of visual studio. Just set the form's property Localizable to true and change the Language property to the one you want to translate. Then, every change you make will only be reflected in the language you selected
If you want more help, you can take a look at this walkthrough

Silverlight ChildWindow with DataTemplate

I am creating a custom ChildWindow that I want to use with a DataTemplate.
The DataTemplate will apply to the "body" of the window, but then, separate from that, I want to always display two buttons, "Save" and "Cancel".
I have no idea how to accomplish that... Any help would be greatly appreciated!
Grab a copy of your ChildWindowStyle from your SdkStyles.xaml to give you a foundation for building your custom control template on. To keep a DataContext you could throw it in a UserControl as UserControl.Resources or if you're just populating ContentPresenters etc you can put the template in your own resource dictionary or wherever you like (though you might want to specify a unique x:Key name for it.) Just depends on how you'd like to use it.
Make your desired changes to the template and also add your Buttons etc. Then you can either set it as the default by replacing the Default BasedOn value in your resource dictionary to point to it or call that style explicitly.
Personally I prefer Expression Blend for all of this and there's even some tutorials out there to help you along with a Web Search (which I might suggest first next time.) Like what you might find here... Hope this helps! :)

Some questions about multi language

Recently, I decided to add 4 languages to my application.
I read about how to do it and I succeed.
But there are two problems/questions I would like to ask.
First question: Is there a better way to change the text of each control instead
private System.Resources.ResourceManager rm;
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr");
rm = new System.Resources.ResourceManager(typeof(MainForm));
and then for each control to write this line:
aboutToolStripMenuItem.Text = rm.GetString("aboutToolStripMenuItem.Text");
addTaskToolStripMenuItem.Text = rm.GetString("addTaskToolStripMenuItem.Text");
addTaskToolStripMenuItem1.Text = rm.GetString("addTaskToolStripMenuItem1.Text");
...
Second Question: lets say the text of label1 is "test" and in other language its "testest" then the size of the label will change, which is ok. but If I got label2 that his location is near label1, label1 might be on the top of label2. How can I move the label1 compare with label2 so no matter how long the text in label1 will be, label2's location will be relative to label1. I dont want to use calculations in the program, I want to know if there's other way like property in one of the controls.
EDIT:
after long thinking, I decided to use XML for my multilanguage. this way I can let the people translate it and upload it for me plus I can use it on runtime instead or reload the programs.
About the relative pos of controls I will use FlowLayoutPanel or TableLayoutPanel I will check further which is better.
Satellite assemblies is what you are looking for.
Q1: In VS, set your form's Localizable property to true. Then select the language property accordingly and type your translations in the designer. That way, you simply need to set the thread culture at start-up and .NET will load the correct language for you. No need to add extra code.
Q2: Again, after you selected the language in the designer, just move the controls around: Their new location/size is part of the translation and will be handled automatically by .NET.
It's not a very good idea change the GUI to a different culture while it's running, it's better to say something like you need to restart the application to see changes.
Although if you need to do it, you need to reload all the resources from the new culture (more or less the same than the InitializeComponen does), not only the text, because the location, size and so on, may be changed too. Also you need to change the thread culture in order to the errors, message and the new controls have the correct culture too (to show them in the correct language too).
You can set your application culture with:
CultureInfo appCulture = CultureInfo.CreateSpecificCulture("fr");
Thread.CurrentThread.CurrentCulture = appCulture;
Thread.CurrentThread.CurrentUICulture = appCulture;
You need an specific culture to use it on formating and parsing.
In answer to your first question:
If you really want to follow that scheme maybe using Reflection or automatic code generation is an alternative for easier management. I usually write my own GetString method that takes a default english string as an argument (used if no resource can be loaded dynamically for the current language). But I am not sure if it is the best solution either...
In answer to your second question:
In Winforms use a TableLayoutPanel or FlowLayoutPanel or another layout component to relatively position the controls. It is possible to specify if the Label fits to its content (AutoSize) for example or if it shall Dock and if yes with what Alignment. There is nearly no use case that would require a tedious self management or computation.
Link: http://msdn.microsoft.com/en-us/library/z9w7ek2f(v=VS.100).aspx

Visual Studio Designer is always trying to change my control

I have a somewhat complex UserControl, and Visual Studio 2008 is giving me a rather harmless annoyance when working with it. Every single time I open the control with the Designer, it decides to immediately change some of the harmless values set by the designer - namely the initialization of Size properties. If I save those changes, close, and reopen, it almost invariably ends up deciding another component of my control needs its initial size changed, ad infinitum. Luckily these changes are harmless since I'm using automatic sizing everywhere, but this is quite annoying to work with. I haven't the foggiest on where to start figuring out what's going wrong, my only thought right now is that the Designer is assigning the results of auto-sizing back into the initial size fields every time I open the control. Any ideas on causes/fixes?
Edit: Also, I am using Application Settings to save sizes of certain resizable child components across runs of the application, but I really hope the Designer is smart enough to understand that it should only ever be using the defaults.
Maybe it can help:
I noticed that FormDesigner (no WPF, no Web etc) has a strange behaviour if you insert one custom UserControl.
There is a random change of other controls (GroupBox, EditBox, ComboBox) size (to me happened with width).
The controls choosen to resize seems to be random, but across restarting of vs2010 it is always the same. If deleted and reinserted, the designer chooses a different control do randomly resize...
I changed the property AutoScaleMode of my UserControl from "Font" to "Inherit" and it did not happen again.
You're right, the designer often tries to add default values to properties.
Add this on top of the property declaration:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
That will tell the designer to ignore this property.
I have somewhat similar problem. I am using Infragistics GroupBox on a user control which I inherited and now want to change its look and feel in the derived class. I have made it protected in base class -- so it does allow me changing properties in derived class. But it does not save it. Every time I open it -- I get same old values of base class back.
Any idea?
Edit: I figured it out.
Trying various value for one of the above given answers.
Using [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] instead of [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] generates code for changed properties - and things work as desired.
Try overriding the DefaultSize property of your control.
From MSDN:
The DefaultSize property represents the Size of the control when it is initially created.

Categories

Resources