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>

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

4

3 回答 3

2

我刚刚遇到了同样的问题,并通过使用 DataType="models:Account" 解决了

<DataTemplate DataType="models:Account" >
    <StackPanel Orientation="Horizontal">
        <TextBox Text="{Binding}" BorderThickness="0"/>
    </StackPanel>
</DataTemplate>
于 2013-10-19T16:09:48.637 回答
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>
于 2013-02-14T02:17:14.433 回答
0

我通过将 ProjectScores 的通用集合更改为 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>

这解决了我的问题。

于 2013-02-14T20:09:49.820 回答