Results 1 to 2 of 2

Thread: WPFisms: Tips, Tricks, and Changes from WinForms

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    WPFisms: Tips, Tricks, and Changes from WinForms

    Ok so the idea of this thread is to list all the little changes that could cause frustration when people are moving from WinForms to WPF. There are also some tips and tricks that are too small to be added to the code bank. Please post if you have any more and I will add them to the list.



    Note: If you have a code example to add to your WPFism post it below if it is short. If it is a full tutorial or rather large please post in the code bank and I will link to it. If you are unsure PM me the example and I will help you decide where it should go. The idea is to keep the requests to the moderators to move posts about to a minimum, thanks guys!

    Control Tips, Tricks and Examples:

    Where is my GridView?
    WPF does not have a gridview by default. One can be downloaded from codeplex (see my sig). I do encourage you to not bother though. In WPF the listbox is now the powerhouse control. This is due to the fact that not only can you style a listbox to fit your data needs but you can also style each individual list item differently too. Styles are a major part of being a successful WPF designer so please take the time to learn them. Chris128 has posted a great alternative here. Submitted By Chris128

    What happened to the Timer component?
    Instead of using the normal Windows.Forms.Timer component, you now use the DispatcherTimer class. See code sample below. Submitted By Chris128
    VB Code:
    1. 'Declare our timer at class level
    2. Private WithEvents MyTimer As New System.Windows.Threading.DispatcherTimer
    3.        
    4. 'Window_Loaded event - Very similar to the Form_Load event in winforms
    5. Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    6.  
    7.      'Set the timer to tick every 3 seconds
    8.      MyTimer.Interval = TimeSpan.FromSeconds(3)
    9.      'Could alternatively use MyTimer.IsEnabled = True
    10.      MyTimer.Start()
    11.  
    12. End Sub
    13.        
    14. 'Our timer's tick event. This gets raised every 3 seconds in this example
    15. Private Sub MyTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTimer.Tick
    16.  
    17.      Button1.IsEnabled = Not Button1.IsEnabled 'Just something to do.. enabled/disable a button
    18.  
    19. End Sub

    How do I change a progress bar to use the Marquee animation style?
    As the progressbar control in WPF has no Style property like it did in Winforms, you can set the property named "IsIndeterminate" to True to make the progressbar use a continuous marquee style animation. Submitted By Chris128

    Why does the Button control have no Enabled property?
    You now use the IsEnabled property instead. Submitted By Chris128

    What happened to the ListViewSubItem class?
    You don't need it as you just bind your listview to a public collection and then specify which column should display which property of the items in your collection. Submitted By Chris128

    How can I change the colour of selected items in a listbox?
    As the ListBox in WPF can be used for so many things, you will often find that you don't want the default blue background to appear whenever you click on an item. To change this you need to override the standard Highlight Brush in your own Style. Then you can set your listbox to use this style for its items by setting the ItemContainerStyle to that style, code example below. Submitted by Chris128
    Code:
    <Style x:Key="TransparentHighlightStyle" TargetType="ListBoxItem">
         <Style.Resources> 
             <SolidColorBrush x:Key=" {x:StaticSystemColors.HighlightBrushKey}"Color="Transparent"/>
         </Style.Resources> 
    
         <!---Rest of your Style Triggers/Setters etc go here-->
    </Style>

    General Tips, Tricks and Examples:


    How do I remove the min/max buttons from a window?
    These propeties have been merged in WPF to the Window.ResizeMode property. This property takes an enumeration and the values can be: NoResize, CanResize, CanResizeWithGrip and CanMinimize. I trust you will find every thing you need with those values. Submited By DeanMc

    Are pixels till the unit of measurement in WPF?
    No. Pixels have been dropped in favour of DIU or device independent units. The idea of this is that your application will look the same at different resolutions. The standard PC/Laptop resolution is 96DPI and consequently the size of one DIU is 1/96th of an inch, so while you will technically not be developing in pixels you can treat 1 DIU as one pixel for most users of your application. Submitted By DeanMc




    Quirks, Bugs and Fixes:

    3:I got a runtime error when I renamed my window?
    This is because the StartUp URI in the application.xaml file needs to be change to match the new name of your window. See my blog post for detailed walk through and video example. Submitted By DeanMc
    Last edited by mendhak; Jan 31st, 2009 at 05:42 PM. Reason: Grammar, spelling, minor changes for 'merger' with tutorials thread

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WPF'isms: Things that can cause frustration please read

    UPDATE: Added a third part to my DataGrid/ListView tutorial, this third part focuses on extending the existing control we built in the first two parts so that it now uses TextBoxes to contain each of its items instead of just un-editable text. This way we can let the user edit the data by typing in to the item textboxes and it will automatically update our collection in the code-behind.
    Last edited by chris128; Jan 26th, 2009 at 07:31 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width