【问题标题】:Setting DataGrid ItemsSource causes XamlParseException设置 DataGrid ItemsSource 会导致 XamlParseException
【发布时间】:2020-07-19 23:45:04
【问题描述】:

所以我使用的 XAML 文件有问题;我正在尝试使用 DataGrid 为用户选择的元素添加属性的表格视图。我目前尝试这样做的方式是,我有一个列表,其中包含在用户单击时填充的适当对,然后将 ItemsSource 设置为该列表。我已经尝试更改此实现的细节(绑定 ItemsSource 而不引用数据网格本身等,但迟早它们似乎都会遇到相同的错误)

奇怪的事情(对我来说)是,在不同的元素上单击几下(并在弹出异常时点击“继续”)后,网格确实填充了数据,尽管它似乎经常“冻结”(显示相同在最后刷新几个元素之前的几个元素的数据,没有抛出异常,但行为肯定不一致)

.xaml.cs

// ParameterPair is a custom class that contains 2 string fields (name, value)
        public List<ParameterPair> AllParameters { get; private set; } = new List<ParameterPair>();


                // called (only) when a new element is click
                // ... code to populate AllParameters here
                // definitely populates properly, checked through debugging
                this.dGrid.ItemsSource = AllParameters;

.xaml

<Page ...>
<Grid HorizontalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <TabControl>
        <TabItem Header="Add Constraint">
            <Grid Name="loginBlock" Grid.Row="0">                        

        <GroupBox Header="Properties"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Top"
                      Margin="10, 10, 10, 0">
                        <StackPanel>


                            <controls:DataGrid x:Name="dGrid" 
                            Height="300" Margin="12"
                            AutoGenerateColumns="true"
                        ItemsSource="{Binding}"
                                               />
                    </StackPanel>
            </GroupBox>
            </Grid>
        </TabItem>
        <TabItem Header="Manage Constraints" />
    </TabControl>

</Grid>
</Page>

【问题讨论】:

    标签: wpf xaml revit-api


    【解决方案1】:

    好的,所以我修好了。原来 AutoGenerateColumns="true" 是罪魁祸首。我很沮丧地遇到了this thread,并尝试手动设置列,这似乎工作得很好,只需将列绑定到用于列表的类中的适当字段。

                                <DataGrid x:Name="dGrid"
                                Height="300" Margin="12"
                                AutoGenerateColumns="false"
                            ItemsSource="{Binding}"
                                                   >
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                                        <DataGridTextColumn Header="Value" Binding="{Binding Value}" />
                                    </DataGrid.Columns>
                                </DataGrid>
    

    附:我会避免选择这个作为我的答案,因为它主要是一种解决方法,希望更有经验的人会来提供更彻底和简洁的解决方案(也许我自己稍后会在这个项目中......)

    【讨论】:

      【解决方案2】:

      错误似乎不是来自您的数据绑定,而是来自某处的 xaml 标记。 您的 GroupBox 似乎没有右括号。 这是一个自定义的 DataGrid 吗?因为它与您的其他控件不同,它被称为“控件:DataGrid”。它的标记可能有问题。

      【讨论】:

      • 我也尝试删除“控件:”部分(正如我最初在教程中看到的那样,我自己不确定为什么会这样),但没有任何改变。打开的 GroupBox 只是我的格式错误(对不起!)。我尝试删除除页面之外的所有内容(并保留 DataGrid),但仍然出现错误。我同意这可能是 xaml 的问题,因为我很遗憾地是一个完整的新手。当我环顾四周时,唯一看起来充满希望的事情可能与正确导入库等有关,但我无法实现。
      • 而且它不是自定义的DataGrid no
      • 已修复,谢谢,您的评论是一个很好的提示。我想当不祥的异常弹出时,我会尝试更多地查看 XAML 文件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 2011-03-03
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多