views:

183

answers:

2

Hello,

when I d*ouble-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

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

+2  A: 

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.

Josh Einstein
@JoshYou 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 => http://stackoverflow.com/questions/2404736/wpf-richtextbox-typing-twice-slower-than-in-a-vb-5-0-richtextbox
msfanboy
A: 

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!

Simon_Weaver