Monday, June 20, 2011

First WP7 App

Hey everyone. I decided to try some Windows Phone 7 development to see how easy or hard it is. So far, I have found it to be pretty easy since I’m a WPF and Silverlight developer. If you are a .Net developer and are interested in mobile development, I would highly encourage you to give WP7 development a try.

Here is a very simple application I wrote just to get my feet wet with WP7 development. It’s available as a free download in the marketplace, so if you have a Windows Phone 7 device, give it a shot and let me know what you think. Again, it is an extremely simple application, but if you are a baseball fan, it should be useful.

LargePcAppTile

screenshot1screenshot2

Let me know what you think.

Thanks!

WPF Selected Treeview Item Background

This one took me a while to figure out, so I thought I’d share it with everyone. If you have a treeview item and don’t want the selected item to be that ugly blue color, you can easily change it.

 

All you need to do is add this line of code into your Treeview. In this particular example, I didn’t want the selected item to be colored at all, so I set the color to Transparent, but you can enter other colors here to suit your app’s needs.

Hope this helps. Happy coding!

                    <TreeView.Resources>
<
SolidColorBrush Color="Transparent" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
</
TreeView.Resources>



Wednesday, February 2, 2011

How to Properly Sort on a WPF DataGrid Column in Code

There are a lot of blog posts and articles out there on how to apply a sort to a datagrid in code, but there are very few out there that show you how to do it properly. Most of the posts I ran into show you how to apply the sort, but the column header does not update properly. The items are sorted, but the sort indicator (tiny triangle on the header)

image

Does not appear. This can be really confusing especially if you have already sorted by another column by clicking on the column header. This makes the sort indicator appear on that column, but if you sort by a different one in the code behind, the sort indicator still shows that the grid is sorted by the first column (Thanks WPF DataGrid! Nice one!)

Well, there is a way to get it working properly. Here it is.

        private void applySortDescriptions(DataGridColumn col, ListSortDirection listSortDirection)
        {
            //Clear current sort descriptions
            MyDataGrid.Items.SortDescriptions.Clear();

            //Get property name to apply sort based on desired column
            string propertyName = getSortPropertyName(col);           

            //Add the new sort description
            MyDataGrid.Items.SortDescriptions.Add(new SortDescription(propertyName, listSortDirection));
           
            //apply sort
            applySortDirection(col, listSortDirection);           
           
            //refresh items to display sort
            MyDataGrid.Items.Refresh();
        }

        private string getSortPropertyName(DataGridColumn col)
        {
            //place logic in here that will return the name of the property to sort by (ex: return “name”; if you are sorting by the name property)

            return string.Empty;
        }

        private void applySortDirection(DataGridColumn col, ListSortDirection listSortDirection)
        {
            foreach (DataGridColumn c in PatientsViewDatGrid.Columns)
            {
                c.SortDirection = null;
            }
            col.SortDirection = listSortDirection;
        }

That should do it. Now you can sort and the column headers will show the sort indicator appropriately.

Monday, January 3, 2011

How to Create a Splash Screen for a WPF App

Creating a Splash Screen for a WPF application is extremely simple. Simply add your image into your project and set the build action to SplashScreen.

image

That’s pretty much all it takes. When you start up your application, you will see your splash screen image appear while the application is initializing.

You can also add a a new item to your project and select SplashScreen from the available templates

image

This will add a default image which you can then swap for your own, custom splash screen.

Yet another approach would just be to use the Splash Screen APIs. You can create a new SplashScreen object and call it’s Show method to display it and it’s Close method to, you guessed it, close it.

Happy Coding!

 

Wednesday, December 15, 2010

WPF VisualStateManager GotoState Bug

So, I just wasted a couple of hours trying to figure this one out so I thought I’d post about it so you guys don’t have to waste as much time as I did on it.

The WPF VisualStateManager implementation is still not as rock solid as Silverlight’s implementation (ok, let’s face it, Silverlight’s implementation is far from rock solid too, but it is much better of at this point). If you are trying to call the VisualStateManager.GotoState Method on a Window, you are going to be very very frustrated. Why? Because it doesn’t work. If you are inside a user control, it is fine, but inside of a Window… nope!

Instead of calling this

VisualStateManager.GoToState(this, "stateName", true);


You are going to need to call this



VisualStateManager.GoToElementState(this.RootElement as FrameworkElement, "stateName", true);


That should do it for you. Microsoft is aware of the issue and has promised to fix this, but who knows when that’s going to happen.



Happy state-changing!



 




WPF - Accessing Resource Dictionaries in Separate Assemblies

There are certain situations where you will need to access resource dictionaries outside of your application. For example, you may want to share one resource dictionary among several applications that share the same styles.

What you want to do is use WPF’s pack URI Scheme.

Remember to include a reference of the assembly containing the resource dictionary (YourAssembly) in the application project. Then, all you have to do is use this rather strange looking syntax to access that resource dictionary within your xaml.

<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Subfolder/YourResourceDictionary.xaml"/> 

There are many variations of this syntax that you can use. Which one is right for you depends on exactly what you are trying to do. The above syntax should work for a majority of the situations. Accessing these resources can be done in both xaml and the code behind.

For more information on the pack URI Scheme syntax, follow this link:

http://msdn.microsoft.com/en-us/library/aa970069(v=vs.85).aspx

Note - this syntax is not only for resource dictionaries. This is useful for accessing several types of data files like images for example. This is commonly used for accessing resource dictionaries, but it is not limited to that.