Welcome to the Xceed Community | Help
Community Search  
More Search Options

Creating a value-changed handler

Sort Posts: Previous Next
  •  10-09-2008, 9:39 AM Post no. 28190

    Creating a value-changed handler

    The following example demonstrates how to create a DependencyPropertyDescriptor for the ItemsSource property that will provide a value-changed handler so that we can be notified when the value of the ItemsSource property is changed.

    The value of the ItemsSource property is changed to the Employees table when the button located above the grid is clicked.

    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
      <Grid.Resources>
        <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                        Source="{Binding Source={x:Static Application.Current}, Path=Orders}"/>
      </Grid.Resources>

      <DockPanel>
        <Button Content="Change data source"
                DockPanel.Dock="Top"
                Click="ChangeDataSource"/>

          <xcdg:DataGridControl x:Name="OrdersGrid"
                                ItemsSource="{Binding Source={StaticResource cvs_orders}}"
                                DockPanel.Dock="Bottom"/>
      </DockPanel>
    </Grid>

    The DependencyPropertyDescriptor for the ItemsSource property is defined in the OnInitialized override and will call the OnDataGridItemsSourceChanged method when the ItemsSource property has been changed. The ChangeDataSource method will be called by the button to change the ItemsSource property.

    VB.NET

    Protected Overrides Sub OnInitialized( ByVal e As EventArgs )
      MyBase.OnInitialized( e ) 

      Dim gridItemsSourceDescriptor As DependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty( DataGridControl.ItemsSourceProperty, GetType( DataGridControl ) )
      gridItemsSourceDescriptor.AddValueChanged( Me.OrdersGrid, New EventHandler( AddressOf OnDataGridItemsSourceChanged ) )
    End Sub

    Protected Sub OnDataGridItemsSourceChanged( ByVal sender As Object, ByVal args As EventArgs )
      ' Before assigning the ItemsSource, UpdateLayout must be called to ensure that the
      ' DataGridControl's template is created and sized correctly in the page or window.
      Me.OrdersGrid.UpdateLayout()

      Dim column As Column
      For Each column In Me.OrdersGrid.Columns
        Dim width As Double = column.GetFittedWidth()
        If width > -1 Then
          column.Width = width
        End IF
      Next column
    End Sub

    Private Sub ChangeDataSource( ByVal sender As object, ByVal e As RoutedEventArgs )
      Me.OrdersGrid.Columns.Clear()
      Me.OrdersGrid.ItemsSource = Nothing     
      Me.OrdersGrid.ItemsSource = New DataGridCollectionView( App.Employees.DefaultView )
    End Sub

    C#

    protected override void OnInitialized( EventArgs e )
    {
      base.OnInitialized( e );
      DependencyPropertyDescriptor gridItemsSourceDescriptor = DependencyPropertyDescriptor.FromProperty( DataGridControl.ItemsSourceProperty, typeof( DataGridControl ) );
      gridItemsSourceDescriptor.AddValueChanged( this.OrdersGrid, OnDataGridItemsSourceChanged );
    }

    protected void OnDataGridItemsSourceChanged( object sender, EventArgs args )
    {
      // Before assigning the ItemsSource, UpdateLayout must be called to ensure that the
      // DataGridControl's template is created and sized correctly in the page or window.
      this.OrdersGrid.UpdateLayout();

      foreach( Column column in this.OrdersGrid.Columns )
      {
        double width = column.GetFittedWidth();
        if( width > -1 )
        {
          column.Width = width;
        }
      }
    }

    private void ChangeDataSource( object sender, RoutedEventArgs e )

    View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.