WPF:System.ArgumentException => {"'{0}'不是 Visual 或 Visual3D。”}

发布于 2024-08-23 07:34:20 字数 1691 浏览 4 评论 0 原文

当我双击 - 或在已经聚焦时单击一次 - 在我的 DataGridTemplateColumn 内的列表框的空白区域中的项目下方,然后我收到上述错误消息。

我做错了什么?

这是我的代码:

<DataGridTemplateColumn Width="0.3*" Header="Attachments">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <Button>Add</Button>
                <Button>Delete</Button>
                <ListBox Name="itemListBox" BorderThickness="0" ItemsSource="{Binding Attachments}" >                                   
                    <ListBox.ItemTemplate>
                        <DataTemplate>                                           
                            <StackPanel Orientation="Vertical" Margin="5">                                                
                                <TextBlock Text="{Binding DocumentFilename}" />
                            </StackPanel>                                            
                        </DataTemplate>
                    </ListBox.ItemTemplate>                                     
                </ListBox>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> 

注意我在“myPhotos.png”项目条目下方单击的图像: 替代文本
(来源:666kb.com

编辑:此错误也已经通过工具提示在 XAML 中可见,只是还没有看到该错误工具提示...

when I double-click - or click once when its already focused - below the items in a empty area of the Listbox which is within my DataGridTemplateColumn then I get the above error message.

WHAT do I wrong?

This is my Code:

<DataGridTemplateColumn Width="0.3*" Header="Attachments">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <Button>Add</Button>
                <Button>Delete</Button>
                <ListBox Name="itemListBox" BorderThickness="0" ItemsSource="{Binding Attachments}" >                                   
                    <ListBox.ItemTemplate>
                        <DataTemplate>                                           
                            <StackPanel Orientation="Vertical" Margin="5">                                                
                                <TextBlock Text="{Binding DocumentFilename}" />
                            </StackPanel>                                            
                        </DataTemplate>
                    </ListBox.ItemTemplate>                                     
                </ListBox>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> 

Regard that image where I click below the "myPhotos.png" item entry:
alt text
(source: 666kb.com)

EDIT: this error is also already visible in XAML via tooltip just haven`t seen that error tooltip...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千年*琉璃梦 2024-08-30 07:34:20

这似乎确实是一个错误。我运行了您的重现项目并在引发异常时检查了调用堆栈。它发生在调用 VisualTreeHelper.IsAncestorOf 期间的 DataGridCell.RemoveBindingExpressions 中。当传递的对象不是 Visual 或 Visual3D 时,后一种方法会引发异常。但是 DataGridCell 正在将绑定目标的任何元素传递给它。在您的情况下,这恰好是一个不是从 Visual 派生的 Run。

我想您也许可以通过使用 IValueConverter 创建 FlowDocument 并绑定 RichTextBox.Document 来解决此问题,以便将绑定应用于 RichTextBox。但由于 Document 不是依赖属性,因此它不能成为绑定的目标。

因此,您可能想要做的是创建一个托管 RichTextBox 控件的 UserControl:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Local:HomeworkControl Text="{Binding Homework}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

然后在该用户控件中,您将负责构建 RichTextBox、文档、运行等。不幸的是,我认为这只是一个限制(也称为错误)在 DataGrid 控件中。

That indeed seems to be a bug. I ran your repro project and checked out the call stack when the exception is thrown. It happens in DataGridCell.RemoveBindingExpressions during a call to VisualTreeHelper.IsAncestorOf. The latter method throws an exception when it is passed an object that is not Visual or Visual3D. But DataGridCell is passing it whatever element is the target of the binding. In your case that happens to be a Run which does not derive from Visual.

I was thinking you might be able to work around it by using an IValueConverter to create the FlowDocument and binding RichTextBox.Document so that the binding is being applied to the RichTextBox. But since Document isn't a dependency property, it can't be a target of binding.

So instead what you might want to do is create a UserControl that hosts the RichTextBox control:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Local:HomeworkControl Text="{Binding Homework}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

Then in that user control you would take care of building the RichTextBox, document, run, etc. Unfortunately I think this is just a limitation (aka bug) in the DataGrid control.

老街孤人 2024-08-30 07:34:20

有趣的是,这也发生在我身上。乔什的话引起了我的思考。似乎一旦您选择单元格并再次选择它,它就会尝试加载在我的情况和您的情况中未指定的 CellEditingTemplate ,并且它会引发 Visual/Visual3d 异常。

我通过指定 IsReadOnly< 来修复它/a>="True" 在我的 DataGridTemplateColumn 上。无论如何,我不使用 CellEditingTemplate,因为我正在使用单元格模板中加载的 TextBoxes/DatePicker/Checkboxes 等进行批量插入。

Interestingly this happened to me as well. What Josh said got me thinking. It seems like once you select the cell and select it again it tries to load the CellEditingTemplate which is not specified in my case and yours and it throws the Visual/Visual3d exception.

I got it fixed by specifying IsReadOnly="True" on my DataGridTemplateColumn. I don't use the CellEditingTemplate anyway because I am doing bulk inserts with TextBoxes/DatePicker/Checkboxes etc. loaded in the cell templates.

晨敛清荷 2024-08-30 07:34:20

我对带有自定义列的 Datagrid 遇到了同样的问题,该列带有嵌入运行的超链接,并在运行的 Text 属性上设置了绑定。当运行 Text 绑定未明确设置为 BindingMode.OneWay 时,我收到此错误。明确设置它解决了问题。请注意,在编辑数据网格中的 ANY 列时,我遇到了异常,而不仅仅是这一列。

I had the same problem with a Datagrid with a custom column with a Hyperlink with embedded run, with the binding set on the Run's Text property. When the run Text binding was not explicitly set to be BindingMode.OneWay I got this error. Setting it explicitly solved the problem. Note I got the exception when editing ANY columns in the datagrid not just this one.

能否归途做我良人 2024-08-30 07:34:20

在编辑数据网格中的列时,我遇到同样的错误。这里的xaml列:

 <DataGridTextColumn Header="Precio Unit." Binding="{Binding UnitPrice,StringFormat=0.00}" Width="Auto" MinWidth="115" />

但是错误发生在另一列;这里的 xaml:

                        <DataGridTemplateColumn Header="Descripción" MinWidth="600" Width="Auto" IsReadOnly="True" >
                            <DataGridTemplateColumn.CellTemplate >
                                <DataTemplate >
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                                        <TextBlock Text="{Binding FixedName, Converter={StaticResource toUpperConverter}}" Background="Transparent" 
                                                VerticalAlignment="Center" Margin="0"/>
                                        <TextBlock Margin="5,0,0,0" Foreground="#FFCB6A6A" FontWeight="Normal">
                                            <Run Text="( Stock "/>
                                            <Run Text="{Binding Stock}"/>
                                            <Run Text=" )"/>
                                        </TextBlock>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

错误消失,明确指定:

<Run Text = "{Binding Stock, Mode = OneWay}" />

I get this same error when editing a column in a data grid. here xaml column:

 <DataGridTextColumn Header="Precio Unit." Binding="{Binding UnitPrice,StringFormat=0.00}" Width="Auto" MinWidth="115" />

But the error occurred in another column; here the xaml:

                        <DataGridTemplateColumn Header="Descripción" MinWidth="600" Width="Auto" IsReadOnly="True" >
                            <DataGridTemplateColumn.CellTemplate >
                                <DataTemplate >
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                                        <TextBlock Text="{Binding FixedName, Converter={StaticResource toUpperConverter}}" Background="Transparent" 
                                                VerticalAlignment="Center" Margin="0"/>
                                        <TextBlock Margin="5,0,0,0" Foreground="#FFCB6A6A" FontWeight="Normal">
                                            <Run Text="( Stock "/>
                                            <Run Text="{Binding Stock}"/>
                                            <Run Text=" )"/>
                                        </TextBlock>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

The error disappears specifying explicitly:

<Run Text = "{Binding Stock, Mode = OneWay}" />
凝望流年 2024-08-30 07:34:20

我在 Blend 中经常遇到此错误,但在 DataGrid 中运行时却不会。

我发现编译应用程序(在我的例子中是在 VS 中)并允许 Blend 重新加载 DLL 可以修复它。重新排列列似乎也会触发它自我更新。虽然很痛!

I get this error frequently in Blend, but not at runtime in a DataGrid.

I have found that either compiling the application (in my case in VS) and allowing Blend to reload the DLLs fixes it. Also rearranging the columns seems to trigger it to update itself. Big pain though!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文