Question

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.

Was it helpful?

Solution

You can keep datachart as a named resource in your DockPanel resources and use static resource binding to command parameter. Then use ContentControl to host it.

like this...

    <DockPanel>
            <DockPanel.Resources>
                 <DataChart x:Key="MyDataChart">
                 </DataChart>
            </DockPanel.Resources>
            <DataGrid>
                   <DataGrid.Resources>
                           <CheckBox 
                                 Command="{Binding Command}"
                                 CommandParameter="{StaticResource MyDataChart}" />
                   </DataGrid.Resources>
            </DataGrid>
           <StackPanel>
                 <ChartLegend>
                 </ChartLegend>
                 <ContentControl Content="{StaticResource MyDataChart}"/>
           </stackPanel>
     </DockPanel>

Hoping that you wont use same MyDataChart to 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 CheckBox in your DataGrid resources?

Also your's and mine solution breaks MVVM because we are supplying a UI control (Chart control) to a View Model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top