Sunday, May 31, 2009

WPF Tips And Facts

  • 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:

    new DragDeltaEventHandler(this.OnHeaderResize),

    And handle it:

    private void OnHeaderResize(object sender, DragDeltaEventArgs e)
    Thumb SenderAsThumb = e.OriginalSource as Thumb;
    GridViewColumnHeader header = SenderAsThumb.TemplatedParent as GridViewColumnHeader;

    if (header == null)

    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:

    <ListView Height="200" x:Name="MyListView">
    <CheckBox Checked="AllSelectionChanged" Unchecked="AllSelectionChanged"/>
    <CheckBox IsChecked="{Binding Selected}"/>
    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
    <GridViewColumn Header="Version" DisplayMemberBinding="{Binding Version}"/>
    <GridViewColumn Header="Path" DisplayMemberBinding="{Binding Path}"/>

    You can then handle global check / uncheck like:

    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;

No comments: