Manish Dalal's blog

Exploring .net!

Updated Samples for Silverlight 2

Now that Silverlight 2 has been released, I have updated all the samples. Conversion was mostly straight forward, with very few changes to code.

Building Business Applications with Silverlight 2

Introduces the basic building blocks of a data centric application (primarily using DataGrid control), which can be used to develop full fledge business applications.

Part 1: Adding new item

Part 2: Deleting item

Part 3: Validation (sync)

Part 4: Validation (async, across the wire)

Part 5: Validation (refactored)

Part 6: IEditableObject and Add new item (Take 3!)

Part 7: Beyond Validation, Prevention

All code ported properly except for EndEdit method on DataGrid. DataGrid no longer provides EndEdit method. Instead of calling DataGrid.EndEdit(), we just call EndEdit on the underlying data item that implements IEditableObject interface.

Old Code (Beta 2)

void peopleDataGrid_KeyUp(object sender, KeyEventArgs e) {
    if (Key.Down == e.Key || Key.Enter == e.Key) {
        peopleDataGrid.EndEdit(true, true);
    }
}

New Code (RTW)

void peopleDataGrid_KeyUp(object sender, KeyEventArgs e) {
    if (Key.Down == e.Key || Key.Enter == e.Key) {
        ((Person)peopleDataGrid.SelectedItem).EndEdit();
    }
}

Another change was from DisplayMemberBinding to Binding for data binding.

Source Code for the Business Application:BusinessApp1.zip

Restricting User Input using Attached Property

Highlights use of Attached property to implement text filtering to restrict user input to selected type, such as numeric, decimal and others. Also introduces basics of Attached property and showcases various ways of using them.

Only code change was rename of Source property on KeyEventArgs. Source was renamed to OriginalSource

Old Code (Beta 2)

private static void textBox_KeyDown(object sender, KeyEventArgs e) {
    // bypass other keys!
    if (IsValidOtherKey(e.Key)) {
        return;
    }
    //
    TextBoxFilterType filterType = GetFilter((DependencyObject)sender);
    TextBox textBox = sender as TextBox;
    if (null == textBox) {
        textBox = e.Source as TextBox;
    }

New Code (RTW)

private static void textBox_KeyDown(object sender, KeyEventArgs e) {
    // bypass other keys!
    if (IsValidOtherKey(e.Key)) {
        return;
    }
    //
    TextBoxFilterType filterType = GetFilter((DependencyObject)sender);
    TextBox textBox = sender as TextBox;
    if (null == textBox) {
        textBox = e.OriginalSource as TextBox;
    }

Source Code: FilterService.zip

ComboBox In DataGrid

Examines the usage of ComboBox in DataGrid. In particular, it shows how to implement foreign key scenarios in lieu of SelectedValue/SelectedValuePath property. It also highlights workaround for a bug in that causes ComboBox dropdown to close immediately in DataGrid.

No code change was required to the original RC0 code, just a recompile. But that also means ComboBox in DataGrid still has display bug where it immediately closes. You can use following attached property to force it to open. Please see the original post for full explanation.

public class ComboBoxService {
    public static readonly DependencyProperty ForceOpenProperty =
        DependencyProperty.RegisterAttached("ForceOpen", typeof(bool), typeof(ComboBoxService),
                                            new PropertyMetadata(OnForceOpenChanged));

    public static bool GetForceOpen(DependencyObject d) {
        return (bool)d.GetValue(ForceOpenProperty);
    }

    public static void SetForceOpen(DependencyObject d, bool value) {
        d.SetValue(ForceOpenProperty, value);
    }

    private static void OnForceOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        ComboBox comboBox = d as ComboBox;
        if ((bool)e.OldValue) {
            comboBox.Loaded -= new RoutedEventHandler(comboBox_Loaded);
        }
        if ((bool)e.NewValue) {
            comboBox.Loaded +=new RoutedEventHandler(comboBox_Loaded);
        }
    }

    static void comboBox_Loaded(object sender, RoutedEventArgs e) {
        ComboBox comboBox = sender as ComboBox;
        if (null == comboBox) {
            comboBox = e.OriginalSource as ComboBox;
        }
        //
        comboBox.IsDropDownOpen = true;
    }
}

Usage

<ComboBox ItemsSource="{Binding CityList, Source={StaticResource cityProvider}}" 
          SelectedItem="{Binding CityInfo, Mode=TwoWay}" 
          DisplayMemberPath="CityName"
          src:ComboBoxService.ForceOpen="true"
      />

Tip: There is a quirk in current implementation of ComboBox. If you set SelectedItem before ItemSource, item will not be highlighted in dropdown. Always first set ItemSource and then SelectedItem.

Source Code: ComboBoxUsage.zip

Stealth Paging : DataGrid

Shows how to combine efficient chunked data retrieval of the web, with continuous scroll experience of the desktop to implement stealth or on demand background paging.

No code change was required to the original RC0 code, just a recompile.

Source Code: StealthPaging.zip

Technorati Tags:

Comments

No Comments