开发者

Custom WPF Databinding

I have a List of DataItem's.

DataItem has a RowId, ColumnId and Value.

These dataitems essentially describe the contents of a user definable "table".

Now I need to bi开发者_如何学运维nd this data to a datagrid in such a way that it represents the "table". i.e. the DataItem Value's should be displayed in a grid using their RowId and ColumnId.

Any help with this binding problem would be much appreciated.


If you want to use binding, you'll need an ItemsControl, like ListBox and modify its ItemsPanelTemplate to use Grid. If you don't know the number of columns and rows, unfortunately you'll have to generate them manually from code. Or implement your own Panel completely.

Here's the sample. You'll have to change all bindings from XPath if your underlying model is not XML.

<Window x:Class="CoordBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <XmlDataProvider x:Key="_items">
            <x:XData>
                <Items xmlns="">
                    <Item ColId="1" RowId="2">Blah</Item>
                    <Item ColId="0" RowId="1">Doh</Item>
                    <Item ColId="2" RowId="0">Meh</Item>
                </Items>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>

    <ListBox ItemsSource="{Binding Source={StaticResource _items},XPath=Items/*}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid IsItemsHost="True">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="Grid.Row" Value="{Binding XPath=@RowId}" />
                <Setter Property="Grid.Column" Value="{Binding XPath=@ColId}" />
            </Style>
        </ListBox.Resources>

    </ListBox>

</Window>


OK, this might not be the most "ideal" method (I'd prefer to get it going in XAML), but here's something to get you started. This code starts with a List called Items, and a Grid declared with the name ItemsGrid.

        int maxRow = Items.Select(i => i.RowId).Max();
        int maxCol = Items.Select(i => i.ColumnId).Max();

        for (int i = 0; i <= maxRow; i++)
            ItemsGrid.RowDefinitions.Add(new RowDefinition());

        for (int i = 0; i <= maxCol; i++)
            ItemsGrid.ColumnDefinitions.Add(new ColumnDefinition());

        foreach (var item in Items)
        {
            TextBlock newItem = new TextBlock() { Text = item.Value };
            ItemsGrid.Children.Add(newItem);
            Grid.SetRow(newItem, item.RowId);
            Grid.SetColumn(newItem, item.ColumnId);
        }

Alternatively, you could look into actual DataGrid controls outside of what's cooked into the .NET Framework right now.

The WPF Toolkit has a DataGrid that I've used successfully on several projects: http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117

Third-party vendors also produce simimlar grids. Prominent ones, in no particular order, are Xceed, Telerik, Infragistics, and Syncfusion.

Hope that helps! Tim

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜