Caliburn.Micro icon indicating copy to clipboard operation
Caliburn.Micro copied to clipboard

DataGrid - Event Loaded trows exception "no target found for method"

Open arcuri opened this issue 9 years ago • 8 comments

Hi all,

In my WPF application, when I try to attach to the datagrid event loaded with: cal:Message.Attach="[Event Loaded] = [Action MyDataGridLoaded]"

My action MyDataGridLoaded is not found in my view model. If I try to attach to another event like "GotFocus", the function is called correctly.

Do you have any suggestions???

Regards, Bernardo

arcuri avatar Oct 23 '15 07:10 arcuri

Anyway you could publish some more code. The methods would help as well as how you defined your DataGrid, is it nested?

mvermef avatar Oct 26 '15 19:10 mvermef

This is my code:

<ScrollViewer VerticalScrollBarVisibility = "Auto" HorizontalScrollBarVisibility="Auto">
    <ItemsControl ItemsSource = "{Binding MyList}" >
        < ItemsControl.ItemTemplate >
            < DataTemplate >
                < Expander IsExpanded="True">
                    <DataGrid ItemsSource = "{Binding MyChildList}" AutoGenerateColumns="False" IsReadOnly="True"
                              cal:Message.Attach="[Event Loaded] = [Action MyDataGridLoaded]">

                        <DataGrid.Columns>
                            <DataGridTextColumn Width = "100" Binding="{Binding Number}"      Header="{lex:LocText UI_Number}"/>
                            <DataGridTextColumn Width = "100" Binding="{Binding Name}"        Header="{lex:LocText UI_Name}"/>
                            <DataGridTextColumn Width = "300" Binding="{Binding Description}" Header="{lex:LocText UI_Description}"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Expander>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

arcuri avatar Oct 27 '15 07:10 arcuri

Pretty sure it is related to it being nested in a DataTemplate. If you take the DataGrid out of the DT does it find the method?

Solution will probably be something like this cal:Action.TargetWithoutContext="{Binding DataContext, ElementName=MyItemsControl}" placing this inside of the DataGrid as an attribute and giving the ItemsControl a x:Name (cm will ignore the name as long you don't have anything with that name in your viewmodel)

Is the MyDataGridLoaded a parameterless void?

mvermef avatar Oct 27 '15 21:10 mvermef

Thank you, now it works. Can you explain to me why my solution was wrong??

arcuri avatar Oct 28 '15 07:10 arcuri

It wasn't wrong per se. It's a visual tree limit of data-templates and nesting of controls that CM has issues with. Its been like this for a long time hence the Action extension. Its a binding limitation with how the datacontext is located for the control in the data-template. Since the data-template sort of blocked the binding for the control with the shorthand event handling, CM didn't know what the heck MyDataGridLoaded was, so its subsequent action is to throw an error.

Keep in mind that ContextMenus will also be susceptible to this issue...

-Edit- On side note it might have been the combination of the Expander control and the data-template since the behavior of the expander is to hide its contents by default. Don't really have time to delve into it as them moment.

Glad it is working now.

mvermef avatar Oct 29 '15 15:10 mvermef

The whole target without context is usually to solve problems when the element in question isn't in the visual tree properly like context menus. I don't believe Expander does that however.

I can take a closer look at it if you're able to provide a repro of the problem as I haven't been able to create it. The xaml below works:

<Grid>
    <Expander Header="Test">
        <DataGrid cal:Message.Attach="[Event Loaded] = [Action Test]" />
    </Expander>
</Grid>

nigel-sampson avatar Nov 02 '15 00:11 nigel-sampson

it was also in a datatemplate though.

mvermef avatar Nov 03 '15 07:11 mvermef

Yup, just wanted to spike out whether Expander might be causing it.

nigel-sampson avatar Nov 04 '15 00:11 nigel-sampson