尝试编辑时,WPF DataGrid上动态列的双向绑定异常

时间:2013-02-13 19:54:18

标签: mvvm datagridview datagridtemplatecolumn wpf-4.0

嗨我有以下代码来生成动态列。

     <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>

但是当我尝试编辑一个值时,它会让我“双向绑定需要Path或XPath” 我该如何解决?因为我的单元格值是根据“TitleList”动态加载的,它将动态更改。

3 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,并通过使用DataType =&#34;模型来解决:帐户&#34;

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

答案 1 :(得分:1)

将您的CellEditingTemplate更改为以下

<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>

答案 2 :(得分:0)

我通过将ProjectScores的Generic Colleciton更改为CustomType集合来解决此问题。

在:

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

修复是:

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

在用户界面中:

<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>

这解决了我的问题。