WPF:System.ArgumentException => {“'{0}'不是Visual或Visual3D."} [英] WPF: System.ArgumentException => {"'{0}' is not a Visual or Visual3D."}

查看:215
本文介绍了WPF:System.ArgumentException => {“'{0}'不是Visual或Visual3D."}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我双击(或者当它已经聚焦时单击一次)时,在我的DataGridTemplateColumn内的列表框的空白区域中的项目下方,则出现上述错误消息.

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.

我怎么了?

这是我的代码:

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

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

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

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

推荐答案

那似乎确实是一个错误.引发异常时,我运行了您的repro项目并签出了调用堆栈.在调用VisualTreeHelper.IsAncestorOf的过程中,它发生在DataGridCell.RemoveBindingExpressions中.当后一种方法传递的对象不是Visual或Visual3D时,将引发异常.但是,无论绑定目标是什么元素,DataGridCell都会将其传递给它.在您的情况下,恰好是一个不从Visual派生的运行.

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.

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

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.

因此,您可能想做的是创建一个承载RichTextBox控件的UserControl:

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>

然后在该用户控件中,您将负责构建RichTextBox,文档,运行等.不幸的是,我认为这只是DataGrid控件中的一个局限性(又名bug).

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.

这篇关于WPF:System.ArgumentException =&gt; {“'{0}'不是Visual或Visual3D."}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆