WPF: System.ArgumentException => {"'{0}' is not a Visual or Visual3D."}
Asked Answered
S

5

6

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

Sugarplum answered 3/3, 2010 at 21:46 Comment(8)
can you post the code behind to this?High
there exist no code behind :)Sugarplum
according to this => social.msdn.microsoft.com/forums/en-US/wpf/thread/… I bet I found a BUG !!!Sugarplum
ok here we go with the bounty I have new information I found out: pastebin.com/nsLh2Qn7 there is no Code-behind code except this in the Ctor: customerDG.DataContext = customers; // customers holds 3 customers In general I get this EXCEPTION: SystemArgumentException: {"'{0}' is not a Visual or Visual3D."} INNER EXCEPTION: NULLSugarplum
- When I click in the FirstName cell I get the above Exception - When I click in the LastName cell I get the above Exception - When I exchange the RichTextbox for a Textbox I get NO EXCEPTION clicking in FirstName or LastName cell - When I keep RichtTextbox and set LastName DataGridTextColumnon IsReadOnly=True - AND When I am clicking into it I get no exception but clicking into FirstName DataGridTextColumnon raises an Exception.Sugarplum
Test yourself Visual Studio 2010 RC Project => sendspace.com/file/1z1lhy So, what do I wrong?Sugarplum
@Sugarplum is there a connect issue for this I can vote on?Hartle
I AM SOOOO SICK OF THIS DAMN BUGHartle
R
9

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.

Regeneracy answered 10/3, 2010 at 6:13 Comment(1)
@Regeneracy You seem right, I tried teleriks RadGridView and replaced the DataGrid from M$. Everything works fine no exception. If you check my other Thread even with the RadGridView now the RichTextbox is lagging and I cant type very fast => #2405236Sugarplum
L
6

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.

Luisaluise answered 19/10, 2012 at 13:48 Comment(0)
L
3

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.

Linc answered 17/12, 2015 at 1:33 Comment(0)
D
1

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}" />
Decemvirate answered 28/1, 2016 at 3:32 Comment(0)
H
0

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!

Hartle answered 15/9, 2010 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.