Skip to main content

UnboundDataSource

  • 4 minutes to read

The UnboundDataSource component is designed for unconventional binding scenarios when no strongly typed data set is available at compile time.

Note

The UnboundDataSource is a layer between a data-aware control and the data source.

The following image illustrates the basic functionality of the UnboundDataSource component.

UnboundDataSourceDiagram

Initializing the Unbound Data Source

Note

The Items Source Wizard is not available in the new WPF XAML Designer. For information on the difference between the old and new WPF XAML designers, refer to the Quick Actions and Smart Tags section.

The Items Source Wizard is the most handy way to bind DevExpress data-aware controls to any supported data source type. In this article, binding the Data Grid is taken as an example.

  1. Open the GridControl‘s Smart Tag panel by clicking the icon in the GridControl‘s upper-right corner. Select Items Source Wizard.

    UnboundDataSourceWPF1

  2. Select the “Unbound Data Source” option and click Next.

    UnboundDataSourceWPF2

  3. Select the “Simple Binding” option and click Next.

    UnboundDataSourceWPF2

  4. The Row Count field lets you specify the number of records that the GridControl will display.

    UnboundDataSourceWPF4

After you press Finish, the following XAML will be generated.

<Window ...
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">
    <Window.Resources>
        <dx:UnboundDataSource x:Key="UnboundDataSource" Count="100"/>
    </Window.Resources>
    <Grid>
        <dxg:GridControl AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding Data, Source={StaticResource UnboundDataSource}}">
            <dxg:GridControl.View>
                <dxg:TableView AllowPerPixelScrolling="True" TotalSummaryPosition="Bottom"/>
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</Window>

Mapping the UnboundDataSource to Data

To map the UnboundDataSource to data, populate the UnboundDataSource.Properties collection with UnboundDataSourceProperty objects. Each UnboundDataSourceProperty object identifies a data source field.

The following table lists properties that allow you to associate an UnboundDataSourceProperty object with a data source field.

UnboundDataSourceProperty

property

Description

UnboundDataSourceProperty.Name

Specifies the name of the property represented by the current UnboundDataSourceProperty.

UnboundDataSourceProperty.DisplayName

Specifies the display name of the property.

UnboundDataSourceProperty.PropertyType

Specifies the type of the property.

The following example demonstrates the UnboundDataSource that represents a table with two columns of different types. The Numbers column values are edited using the in-place SpinEdit editor.

<Window ...
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"> 
    <Window.Resources> 
        <dx:UnboundDataSource x:Key="UnboundDataSource" Count="100"> 
            <dx:UnboundDataSource.Properties> 
                <dx:UnboundDataSourceProperty DisplayName="ID" Name="Numbers" PropertyType="{x:Type sys:Int32}"/> 
                <!-- UnboundDataSourceProperty.DisplayName property specifies the default column header -->
                <dx:UnboundDataSourceProperty DisplayName="String Values" Name="Strings" PropertyType="{x:Type sys:String}"/> 
            </dx:UnboundDataSource.Properties> 
        </dx:UnboundDataSource> 
    </Window.Resources> 
    <Grid> 
        <dxg:GridControl AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding Data, Source={StaticResource UnboundDataSource}}"> 
            <!-- UnboundSourceProperty.Name value is used to specify the field name -->
            <dxg:GridColumn FieldName="Numbers" Header="ID">
                <dxg:GridColumn.EditSettings>
                    <dxe:SpinEditSettings/>
                </dxg:GridColumn.EditSettings>
            </dxg:GridColumn>
            <dxg:GridControl.View> 
                <dxg:TableView AllowPerPixelScrolling="True" TotalSummaryPosition="Bottom"/> 
            </dxg:GridControl.View> 
        </dxg:GridControl> 
    </Grid> 
</Window>

Synchronizing Data

The UnboundDataSource requires you to manually process data operations. You can synchronize the GridControl and the data source by handling the following events.

Event Description
UnboundDataSource.ValueNeeded Occurs each time an item is pulled from the data source.
UnboundDataSource.ValuePushed Occurs each time the modified data is pushed to the data source.

Note

When a data-aware control is initially populated, the UnboundDataSource.ValueNeeded event fires each time a value is pulled from the data source.

For example, if you set the row count to 10 and the UnboundDataSource.Properties collection contains 2 UnboundDataSourceProperty objects, the UnboundDataSource.ValueNeeded event will occur 20 times.

The following example demonstrates the UnboundDataSource linked to the ViewModel class that contains sample data.

public class ViewModel {
    //Each data cell is identified by a list index and key value.
    //The key is a string that specifies the column name.
    public List<Dictionary<string, object>> Data { get; set; }

    public ViewModel() {
        CreateList();
    }

    //Populates the list with sample data
    void CreateList() {
        Data = new List<Dictionary<string, object>>();
        //Creates 1000 rows
        for (int i = 0; i < 1000; i++) {
            Data.Add(CreateDictionary(i));
        }
    }

    //Each dictionary object represents a data row.
    //Each dictionary item represents a cell value. It stores a string (column name) and a value (cell value)
    Dictionary<string,object> CreateDictionary(int i) {
        Dictionary<string, object> dict = new Dictionary<string, object>();
        //Specifies the value in the "Strings" column
        dict.Add("Strings", "Value" + i.ToString());
        //Specifies the value in the "Numbers" column
        dict.Add("Numbers", i);
        return dict;
    }
}

The following image demonstrates the result.

UnboundDataSourceWPF Sample Application