0

hi I have the following code to generate dynamic columns.

     <DataGrid AutoGenerateColumns="False" DockPanel.Dock="Top" ItemsSource="{Binding StudentList}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding StudentName}"  
                                Header="Name"/>
            <DataGridTemplateColumn Width="*">
                <DataGridTemplateColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" 
                                Value="Stretch" />
                        <Setter Property="VerticalContentAlignment"  Value="Stretch" />
                        <Setter Property="Margin" Value="0" />
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ItemsControl  ItemsSource="{Binding DataContext.TitleList, ElementName=LayoutRoot}">
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <StackPanel Orientation="Horizontal">
                                                </StackPanel>
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Border  Width="70" >
                                                    <TextBlock Text="{Binding}" TextAlignment="Center"/>
                                                </Border>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGridTemplateColumn.HeaderStyle>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding ProjectScores}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Border Width="70">
                                        <TextBlock Text="{Binding}" TextAlignment="Center"/>
                                    </Border>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBox Text="{Binding}" BorderThickness="0"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

But when i try to edit a value it is throwing me "Two-way binding requires Path or XPath" how do i fix this? cause my cell values are dynamically loaded based on the "TitleList" which will be changed dynamically.

WPFKK
  • 1,419
  • 3
  • 20
  • 42

3 Answers3

2

I just had the same problem and was solved by using DataType="models:Account"

<DataTemplate DataType="models:Account" >
    <StackPanel Orientation="Horizontal">
        <TextBox Text="{Binding}" BorderThickness="0"/>
    </StackPanel>
</DataTemplate>
jleyva
  • 96
  • 6
1

Change your CellEditingTemplate to the following

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <ItemsControl ItemsSource="{Binding Path=ProjectScores}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Width="70">
                        <TextBox Text="{Binding Path=.}" TextAlignment="Center"/>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
Peter Karlsson
  • 1,170
  • 7
  • 15
  • what does the Text="{Binding Path=.}" do? – WPFKK Feb 14 '13 at 20:05
  • It binds to the current datacontext, in this case a "ProjectScore" which I just had as a number in my tests. If the score is an actual object you would put the property that you want to bind to in there – Peter Karlsson Feb 14 '13 at 21:35
  • Basically it is the same as {Binding} but defining a Path as that was what the binding engine was asking for – Peter Karlsson Feb 15 '13 at 07:50
  • Binding Path=. instead of just Binding did the trick for me, tnx – jrb Feb 13 '15 at 11:56
0

I resolved this issue by changing the Generic Colleciton of ProjectScores to a Collection of CustomType.

Before:

public List<string> ProjectScores{get;set;}

Fix is:

public List<StudentScore> ProjectScores{get;set;}
public class StudentScore
{
public ScoreValue{get;set;}
}

And in UI:

<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
    <ItemsControl ItemsSource="{Binding Path=ProjectScores}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border Width="70">
                    <TextBox Text="{Binding Path=ScoreValue}" TextAlignment="Center"/>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</DataTemplate>

That solved my issue.

WPFKK
  • 1,419
  • 3
  • 20
  • 42