How to set Resources for DataGrid.Columns ?

Jun 18, 2009 at 2:48 AM

I want to create a datagrid code-behind!

such as CheckbookManager smple.

I have found some xaml like this:

            <Style x:Key="dgHeaderStyle" TargetType="dg:DataGridColumnHeader">
            <Style x:Key="dgCellStyle" TargetType="dg:DataGridCell"
            <Style x:Key="dgRowStyle" TargetType="dg:DataGridRow">


and I can use upper Styel code-behind, but I can't  create columns like down xaml

            <dg:DataGrid.Columns>
                <dg:DataGridTextColumn Header="No." Width="SizeToCells"  Binding="{Binding CheckNumber}" />

                <dg:DataGridTemplateColumn Header="Date" MinWidth="100">
                    <dg:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <dg:DatePicker SelectedDate="{Binding Date}" SelectedDateFormat="Short" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellEditingTemplate>
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Date, StringFormat=d}" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>

                <dg:DataGridTextColumn Header="Pay To" MinWidth="200" Binding="{Binding Recipient}">
                    <dg:DataGridTextColumn.CellStyle>
                        <Style TargetType="dg:DataGridCell" BasedOn="{StaticResource dgCellStyle}">
                            <Setter Property="FontWeight" Value="Bold" />
                            <Setter Property="Foreground" Value="DarkBlue" />
                        </Style>
                    </dg:DataGridTextColumn.CellStyle>
                </dg:DataGridTextColumn>

                <dg:DataGridComboBoxColumn Header="Memo" Width="*"
                           ItemsSource="{Binding Source={x:Static Data:CheckBook.Descriptions}, Mode=OneWay}"
                           TextBinding="{Binding Memo}">
                    <dg:DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="IsEditable" Value="True" />
                        </Style>
                    </dg:DataGridComboBoxColumn.EditingElementStyle>
                </dg:DataGridComboBoxColumn>

                <dg:DataGridCheckBoxColumn Header="C" Width="SizeToHeader" Binding="{Binding Cleared}" ElementStyle="{DynamicResource NoBorderCheckBoxStyle}" EditingElementStyle="{DynamicResource NoBorderCheckBoxStyle}" />
                <dg:DataGridTextColumn Width="SizeToCells" Header="Payment" Binding="{Binding Amount, Converter={StaticResource amountConverter}, ConverterParameter=0, StringFormat=C}" />
                <dg:DataGridTextColumn Width="SizeToCells" Header="Deposit" Binding="{Binding Amount, Converter={StaticResource amountConverter}, ConverterParameter=1, StringFormat=C}" />

                <dg:DataGridTemplateColumn Width="SizeToCells" Header="Balance" ClipboardContentBinding="{Binding TotalBalance}">
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Foreground="{Binding TotalBalance,Converter={StaticResource BalanceDisplayConverter}}" Text="{Binding TotalBalance, StringFormat=C}" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>

            </dg:DataGrid.Columns>

 

if this columns to create like this :

dataGrid1.Columns.Add(new DataGridTextColumn() { Binding = new Binding("aaa"), Header = "Label" });

dataGrid1.Columns.Add(new DataGridTextColumn() { Binding = new Binding("bbb"), Header = "Count" });

but this:

<dg:DataGridTemplateColumn Width="SizeToCells" Header="Balance" ClipboardContentBinding="{Binding TotalBalance}">
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Foreground="{Binding TotalBalance,Converter={StaticResource BalanceDisplayConverter}}" Text="{Binding TotalBalance, StringFormat=C}" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>

I can't know how to create code-behind!

 

I have ever found a listview columns resources .  so ,  I think this datagrid colums can make a styel resources too ? 

 if can ,then this datagrid is esay to create!

please help me!

Thanks a lot!

Jun 20, 2009 at 2:19 AM

please help me!

Coordinator
Jun 23, 2009 at 5:29 AM

Take a look at this thread, http://wpf.codeplex.com/Thread/View.aspx?ThreadId=34651.