I have an XAML tree as follows:
<Window>
<Grid>
<DockPanel>
<DataGrid>
<DataGrid.Resources>
<CheckBox Command="{Binding Command}" CommandParameter="??" />
</DataGrid.Resources>
</DataGrid>
<StackPanel>
<ChartLegend>
</ChartLegend>
<DataChart>
</DataChart>
</stackPanel>
</DockPanel>
</Grid>
</Window>
I want to have DataChart object as CommandParameter on ViewModel from a Command on DataGrid.
My Findings:
I’m getting DockPanel object as CommandParameter, then I have to apply method FindName("") to get the DataChart. And do further modifications.
But I want the DataChart object directly, to avoid TypeCasting or searching down the Tree.
You can keep datachart as a named resource in your
DockPanelresources and use static resource binding to command parameter. Then useContentControlto host it.like this…
Hoping that you wont use same
MyDataChartto host to another area (as that would result in “visual tree parent disconnect” error).Although I must ask you this… why is there a lonely
CheckBoxin yourDataGridresources?Also your’s and mine solution breaks MVVM because we are supplying a UI control (Chart control) to a View Model.