Monday, November 7, 2011

Framework

List of items that could be useful as a base library for any wpf application.

a) Including an abstract class like BaseNotifier that implements IPropertyChanged interface. Its a good idea for all business objects to inherit this base class.
b) Some abstract base class for viewmodel that can inherit from BaseNotifier. All our view model class can inherit this abstract view model base class. Can also expose a messagebox service implementation into this vew model base. It can also have weakside reference property accessible by child classes.
c) Some base command class implementing ICommand interface
d) Utility class that can be used to pass event invocations on wpf controls into weakside reference method calls
e) to be continued.......  

XamComboEditor - controlling combo Item selections

I needed to control how user can selected items in combo box drop down.
ItemContainerStyle whose value would be a style for target type ComboBoxItem.
IsEnabled property is bound to a MultiValueConverter. datacontext itself, current comboitem were passed as parameters to determine if its selectable.
Additional properties were set disallow keyboard focus, horizontal, vertical content alignment were stretched as below.

 <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="None"/>

<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
 <Setter Property="VerticalContentAlignment" Value="Stretch"/>


               <Setter>
                    <Setter.Property>IsEnabled</Setter.Property>
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource sampleMultiConverter}" Mode="OneWay">
                            <Binding ElementName="xyzCmb" Path="DataContext" />
                            <Binding />
                        </MultiBinding>
                    </Setter.Value>
                </Setter>

Sunday, November 6, 2011

XamDataGrid - Something like near Merge Cells

Merging Cells across rows with same column values

Windows version UltraGrid had simple way of doing this. But this does not exist in XamDataGrid. So the workarounds!!!  There could be other or better ways but I followed this way.

Took hierarchical data approach. Having a DataSet was not an option for me, hence sorted data into parent child lists like below. DataRecordsList was the DataSource for XamDataGrid

public List<HierarchicalNode<T>> DataRecordsList;

public class HierarchicalNode
{
           public List<T> TopNode {get; set;}
           public List<T> ChildNodes {get; set;}
}

UltraGrid had bands concept, but here we have them as FieldLayouts. Defined two field layouts for the parent and child.  Parent FieldLayout needed additional column for ChildNodes and hidden some columns in child FieldLayout which would have been the part of merged cells.

Well this approach needed some ugly settings like adjusting parent and column lengths, hiding child column headers, and structure of data is completely different than what i would have desired. Also the merged cell text is like fixed and cannot be aligned vertically. Would love UltraGrid more than XamDataGrid...

Two Way MultiSelect ListBox

Note: Source of this knowledge was from some blog....

SelectedItems on ListBox is a readonly property. I need to bind a list to multiple selections on ListBox and also list needs to support two way binding.

Took a ListBoxExtension class with two attached properties.
a) One attached property is used to bind to the selectionItems observablecollection in the viewmodel. The property change callback initializes the other dependency property with an instance of a class that keeps track of list changes.


public static readonly DependencyProperty SelectionItemsProperty = DependencyProperty.RegisterAttached("SelectionItems",
                typeof(IList), typeof(ListBoxExtension), new PropertyMetadata(null, new PropertyChangedCallback(OnSelectedItemsChanged)));


b) Other dependency property is used for ListBoxExtensionHelper class implementing IWeakEventListener, that keeps track of change events between our viewModel SelectionItems list and listboxes selectedItems list. ReceiveWeakEvent was interesting here....


private static readonly DependencyProperty ExtensionHelperProperty = DependencyProperty.RegisterAttached(
                "ExtensionHelper", typeof(ListBoxExtensionHelper), typeof(ListBoxExtension), new PropertyMetadata(null));


Saturday, November 5, 2011

XamComboEditor in XamDataGrid Column Samples

a) How to bind an enumeration to a Combo Editor in a grid column

       <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type System:Enum}" x:Key="EnumKey">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="Model:EnumType" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

       <Setter Property="ItemsSource" Value="{Binding Source={StaticResource ResourceKey=EnumKey}}" />

b) How to bind list of objects to a Combo Editor in a grid column

        1) Grid Column Type a simple data type
            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.ListOfCustomObj}" />
            <Setter Property="DisplayMemberPath" Value="NameOfPropertyToDisplay" />
            <Setter Property="ValuePath" Value="PropertyForValue" />

        2) Grid Column Type a custom data type
      <Setter Property="DisplayMemberPath" Value="NameOfPropertyToDisplay" />
      <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.ListOfCustomObj}" />

      Note: These above couple of line may not be sufficient as combo box may be displaying type instead of item, in which case custom Data Type needs to override Equals method