Friday, August 24, 2012

How to Change Application FontFamily at Runtime

This one stumped me for a while. There are a lot of blog posts out there on how to change the font family of a particular style if you know the key, or how to do it for all textblocks in the application. The problem I was facing was that I used a lot of Labels and other controls that did not pick up these changes. Not only that, but I found no way of editing my default styles at runtime. After trying several approaches, I came up with this solution. I don’t know if it will work for everyone, but in my case, it is working beautifully. I hope it helps.


        private void applyFontFamilySettings()
        {
            foreach (DictionaryEntry entry in Application.Current.Resources)
            {
                if (entry.Value is Style)
                {
                    Style originalStyle = (Style)entry.Value;
                    swapStyles(originalStyle, entry.Key, originalStyle.TargetType);
                }
            }
        }

        private void swapStyles(Style s, object styleKey, Type t)
        {
            Style newStyle = new System.Windows.Style()
            {
                TargetType = t
            };

            foreach (Setter st in s.Setters)
            {
                newStyle.Setters.Add(st);
            }

            newStyle.Setters.Add(new Setter(FontFamilyProperty, UiSettings.FontFamily));
            Application.Current.Resources[styleKey] = newStyle;
        }




Thursday, July 5, 2012

How to Create a Custom Stroke in Expression Design

 

1) First, draw whatever you want to be used as a stroke. In this case, I will drew an ant.

image

2) Select the paths with the selection tool and go to Object > Stroke > New Stroke Definition

image

This will open up a new tab that look something like this

image

3) Select everything with the selection tool and right click. You should see this:

image

This gives you the option to anchor the image and make repeating. In my case, I want the ants to repeat, so I select that.

4) File >  Save

image

Give your stroke an appropriate name.

That’s it. Now you should be able to use your stroke in your designs.

image

With this new stroke, I can now make a line of ants following whatever path I want.

image

I hope this helps! Have fun!

Thursday, February 2, 2012

Expression Blend Bug–Can’t Open UserControl

Ever seen this when trying to work in Blend?

(Element Name) is not supported in a windows presentation foundation (WPF) project.

or this?

(UserControl) is not supported in a windows presentation foundation (WPF) project.

Well, I ran into this and had a tough time figuring out what was going on. There are a lot of forum posts out there talking about the topic, but no real solution that I have found. I kept on trying different things and was able to overcome it. Here is what I did to fix it.

I went to the solution explorer in visual studio and right clicked on the project file for the project that was having the problems. I selected ‘Edit Project File’. This unloads the project from the current solution and opens the project file in the xml editor.

Towards the top of the file you should see something like this

image

After my offending project file with other functioning project files, I noticed that my platform was set to x86 rather than AnyCPU like my other projects. I changed it to AnyCPU, saved, reloaded the project, built the solution and then tried to open the UserControl in Blend. This fixed the problem!

If you are having this same issue, I’d give that a shot. Good luck!

Wednesday, January 18, 2012

How to be a Computer Expert!

 

This one is all over the web. Just thought I’d post it here in case you haven’t see it. It sums it up pretty well.

computer flow chart

Wednesday, January 11, 2012

How to Change the Font Size of a WPF Tab Control’s Header

To change the font size of the tab header of a wpf tab control you have to redefine the ItemTemplate property of the Tab Control.

This is a question I have heard a lot, so I decided to blog about it for those of you needing to do the same thing. Here is a code sample.

<DataTemplate x:Key=”temp”>

<TextBlock Text=”{Binding}” FontSize=”20”/>

</DataTemplate>

 

<TabControl …. ItemTemplate=”{DynamicResource temp}”/>

 

That’s all there is to it.

Happy Coding!

Wednesday, December 14, 2011

Reboot from the Command Line

I often use a remote desktop connection to work on my desktop from my laptop. This is very handy, but there are some limitations. One such limitation is the lack of ability to reboot the machine you are remoting into. The option is not available within the start menu when you are remoting. There are some instances, though, when you really need to reboot. If you find yourself in this situation, you can reboot via the command line. Here is the command to do it.

shutdown /r /t 01

Happy Remoting!

Monday, November 28, 2011

How to Create a Simple Splash Screen in WPF

I have posted on this topic before, but I thought I would re-post with a step by step tutorial for those who are very new to wpf. Sorry to those of you who are more veterans. I will post more advanced topics soon, but this one’s for the newbies.

Here goes…

WPF makes creating a splash screen fairly simple. All you need to do is embed an image file into your project and change the Build Action on it.

Here is a step-by-step tutorial on how to do this.

Step 1) Create your application.
Here is the xaml for my very complicated application:

image

Step 2) Add an image file to your application. PNG files with transparent backgrounds are supported and are often a good choice for splash screens. Here is the image I created in Photoshop… nice huh?

image

Step 3) Right click on the image file in the solution explorer and click on Properties

image

Step 4) Set the Build Action to SplashScreen in the Properties panel

image

Step 5) Build and run the application.

 

You should now see your image splash before the application is loaded. I managed to get a screenshot as the splash screen was fading out and my application was starting to fade in.

image

That’s all there is to it. It is very simple. There are other ways to show and hide your splashcreen in code, but I won’t get into that in this post. I just wanted to keep it simple for those of you new to SplashScreens in wpf. Happy Splash Screening!