Click here to Skip to main content
15,886,806 members
Articles / Desktop Programming / WPF

WPF Tips and Facts

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
31 Oct 2011CPOL 6.6K   5  
Some useful WPF tips.
  • Do you know that there is no way (at least from my research and experiments) to hide the control box of a dialog? I tried several ways including using Native GetWindowLong and SetWindowLong to set the border style as fixed dialog but in vein.
  • One of the hardest things in WPF is to prevent re-size of a column in ListView. One way is to completely templatize ColumnHeader and comment out the thumb gripper. This approach is listed here. I like the other approach listed in the article where one can attach a handler like:
  • C#
    MyListView.AddHandler(Thumb.DragDeltaEvent, 
        new DragDeltaEventHandler(this.OnHeaderResize), true);

    And handle it:

    C#
    private void OnHeaderResize(object sender, DragDeltaEventArgs e)
    {
    	Thumb SenderAsThumb = e.OriginalSource as Thumb;
    	GridViewColumnHeader header = 
    	  SenderAsThumb.TemplatedParent as GridViewColumnHeader;
    
    	if (header == null)
    	{
    		return;
    	}
    
    	GridView view = (MyListView.View as GridView);
    
    	// If user tries to resize checkbox column, reset the width to fixed
    	if (view.Columns[0] == header.Column)
    	{
    		header.Column.Width = FixedWidth;
    		e.Handled = true; 
    	} 
    }
  • ListView and CheckBox - Following is a method to add a checkbox both to the header and to the column rows in the ListView:
  • XML
    <ListView Height="200" x:Name="MyListView">
       <ListView.View>
           <GridView>
               <GridViewColumn>
                   <GridViewColumn.HeaderTemplate>
                       <DataTemplate>
                           <CheckBox Checked="AllSelectionChanged" 
                                   Unchecked="AllSelectionChanged"/>
                       </DataTemplate>
                   </GridViewColumn.HeaderTemplate>
                   <GridViewColumn.CellTemplate>
                       <DataTemplate>
                           <CheckBox IsChecked="{Binding Selected}"/>
                       </DataTemplate>
                   </bsp;           </GridViewColumn.CellTemplate>
               </GridViewColumn>
               <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                   <GridViewColumn Header="Version" 
                           DisplayMemberBinding="{Binding Version}"/>
               <GridViewColumn Header="Path" DisplayMemberBinding="{Binding Path}"/>
           </GridView>
       </ListView.View>
    </ListView>
    <gridview allowscolumnreorder="False">
    <gridviewcolumn width="25">
    <gridviewcolumn.headertemplate>
    <listview name="MyListView" height="200">
    <listview.view><gridview>
    <gridviewcolumn><gridviewcolumn.headertemplate>

    You can then handle global check / uncheck like:

    C#
    private void AllSelectionChanged(object sender, RoutedEventArgs e)
    {
    	CheckBox chkBox = sender as CheckBox;
    	if (chkBox != null)
    	{
    		bool check = chkBox.IsChecked.Value;
    
    		foreach ([YourBoundType] selection in MyListView.Items)
    		{
    			selection.Selected = check;
    		}
    	}
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) MixModes Inc. | Research In Motion
Canada Canada
Ashish worked for Microsoft for a number of years in Microsoft Visual Studio (Architect edition) and Windows Live division as a developer. Before that he was a developer consultant mainly involved in distributed service development / architecture. His main interests are distributed software architecture, patterns and practices and mobile device development.

Currently Ashish serves as a Technical Lead at RIM leading next generation BlackBerry media experience and also runs his own company MixModes Inc. specializing in .NET / WPF / Silverlight technologies. You can visit MixModes at http://mixmodes.com or follow it on Twitter @MixModes

In his free time he is an avid painter, hockey player and enjoys travelling. His blog is at: http://ashishkaila.serveblog.net

Comments and Discussions

 
-- There are no messages in this forum --