2011-09-19 37 views
0

我有一個XAML樹如下:UI對象,在XAML樹samelevel,作爲CommandParameter

<Window> 
    <Grid> 
     <DockPanel> 
       <DataGrid> 
         <DataGrid.Resources> 
           <CheckBox Command="{Binding Command}" CommandParameter="??" /> 
         </DataGrid.Resources> 
       </DataGrid> 
       <StackPanel> 
        <ChartLegend> 
        </ChartLegend> 
        <DataChart> 
        </DataChart> 
       </stackPanel> 
     </DockPanel> 
    </Grid> 
</Window> 

我想有​​對象CommandParameterViewModelCommandDataGrid

我的發現:

我越來越DockPanel對象作爲CommandParameter,然後我需要去申請方法FindName("")得到​​。並做進一步的修改。

但我想要​​對象直接,以避免TypeCasting或搜索樹。

回答

0

您可以在您的DockPanel資源中將datachart保存爲命名資源,並使用靜態資源綁定命令參數。然後使用ContentControl來承載它。

這樣的...

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

希望你不會使用相同的MyDataChart託管到另一區域(因爲這將導致「可視化樹父斷開」的錯誤)。

雖然我必須問你這個...爲什麼你的DataGrid資源中有一個孤獨的CheckBox

此外,您的和我的解決方案打破了MVVM,因爲我們正在向視圖模型提供UI控件(圖表控件)。

+0

這是XamDataChart的一個主要缺點,即ItemSource無法通過Binding更新,因爲它是ReadOnly屬性。它只能通過帶UI控制的代碼進行更新。 –

+0

但是這樣做的工作? –

+0

只適用於它。會更新你。 –