开发者

UI object, on samelevel of XAML Tree, as CommandParameter

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜