2012-10-25 38 views
0

在我的一個視圖中,我將ContentControl綁定到定義爲同一頁面中資源的DataTemplate。它裏面,我有通過設置DataTemplate中的觸發器中聲明的綁定屬性在充滿不同CONTROLTEMPLATES(四)個運行時間控制:MVVM - 設置具有DataGrid的ControlTemplate時的NullReferenceException

<DataTemplate x:Key="DeviceSetupDataTemplate"> 
     <Control x:Name="theControl" Template="{DynamicResource BanknotesReaderControlTemplate}" /> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding DeviceSetup.ShowBanknotesReaderControlTemplate, Source={StaticResource Locator}}" Value="true" > 
       <Setter TargetName="theControl" Property="Template" Value="{DynamicResource BanknotesReaderControlTemplate}" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding DeviceSetup.ShowCoinsReaderControlTemplate, Source={StaticResource Locator}}" Value="true" > 
       <Setter TargetName="theControl" Property="Template" Value="{DynamicResource CoinsReaderControlTemplate}" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding DeviceSetup.ShowHoppersControlTemplate, Source={StaticResource Locator}}" Value="true" > 
       <Setter TargetName="theControl" Property="Template" Value="{DynamicResource HoppersControlTemplate}" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding DeviceSetup.ShowCoinsCashBoxControlTemplate, Source={StaticResource Locator}}" Value="true" > 
       <Setter TargetName="theControl" Property="Template" Value="{DynamicResource CoinsCashBoxControlTemplate}" /> 
      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 

...和我的網格中的內容控制:

<ContentControl ContentTemplate="{DynamicResource DeviceSetupDataTemplate}" Margin="5" /> 

在我的兩個CONTROLTEMPLATES我有一個DataGrid的,一定到列表我的自定義對象(這裏有其中之一):

<ControlTemplate x:Key="BanknotesReaderControlTemplate" TargetType="{x:Type Control}"> 
     <Grid d:DesignWidth="772.02" d:DesignHeight="532.76" Margin="15,0"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="0.08*" /> 
       <RowDefinition Height="0.1*" /> 
       <RowDefinition Height="0.1*" /> 
       <RowDefinition Height="0.1*" /> 
       <RowDefinition Height="0.1*" /> 
       <RowDefinition Height="0.08*" /> 
       <RowDefinition Height="0.44*" /> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="35*" /> 
       <ColumnDefinition Width="40*" /> 
       <ColumnDefinition Width="25*" /> 
      </Grid.ColumnDefinitions> 
      <StackPanel Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center" > 
       <Button Margin="0,3,10,3" Command="{Binding DeviceSetup.AddBanknoteType, Mode=OneWay, Source={StaticResource Locator}}" Content="{DynamicResource 442}" Width="85" Style="{DynamicResource DeviceSetupButtonStyle}" Height="Auto" Padding="2,1" /> 
       <Button Margin="0,3,10,3" Command="{Binding DeviceSetup.ChangeBanknoteType, Mode=OneWay, Source={StaticResource Locator}}" Content="{DynamicResource 444}" Width="85" Style="{DynamicResource DeviceSetupButtonStyle}" Padding="2,1" /> 
       <Button Margin="0,3" Command="{Binding DeviceSetup.DeleteBanknoteType, Mode=OneWay, Source={StaticResource Locator}}" Content="{DynamicResource 443}" Width="85" Style="{DynamicResource DeviceSetupButtonStyle}" Height="Auto" Padding="2,1" /> 
      </StackPanel> 
      <DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Top" HorizontalContentAlignment="Center" AutoGenerateColumns="False" ScrollViewer.VerticalScrollBarVisibility="Hidden" Grid.Row="6" Grid.ColumnSpan="3" Margin="0,5" 
         ItemsSource="{Binding DeviceSetup.BanknotesDetails, Source={StaticResource Locator}}" SelectedItem="{Binding DeviceSetup.SelectedBanknoteDetails, Source={StaticResource Locator}}" > 
       <DataGrid.Columns> 
        <DataGridCheckBoxColumn Binding="{Binding IsEnabled}" Width="5*" /> 
        <DataGridTextColumn Binding="{Binding Value}" IsReadOnly="True" Header="{DynamicResource 445}" Width="20*" ElementStyle="{DynamicResource SmallSizedTextBlockBlackPlusMatic}" /> 
        <DataGridTextColumn Binding="{Binding Currency}" IsReadOnly="True" Header="{DynamicResource 446}" Width="25*" ElementStyle="{DynamicResource SmallSizedTextBlockBlackPlusMatic}" /> 
        <DataGridTextColumn Binding="{Binding MoneyCode}" IsReadOnly="True" Header="{DynamicResource 447}" Width="50*" ElementStyle="{DynamicResource SmallSizedTextBlockBlackPlusMatic}" /> 
       </DataGrid.Columns>  
      </DataGrid> 
     </Grid>  
    </ControlTemplate> 

而現在的問題:在視圖我有四個按鈕通過命令連接到viewModel,在它內部的callBack中,我更改屬性的布爾值以更改控件模板。 有時,似乎隨機,我得到一個NullReferenceException當我設置爲true的布爾屬性,以顯示數據網格內的控制模板。 MoreOver,在異常的堆棧跟蹤中,我找到了對DataGridCellItemAutomationPeer的引用...我認爲這可能是一個很好的調查起點,但真誠地我不知道該怎麼做。

謝謝advace ..

+0

我現在發現,如果我在更改控制模板時選擇一行數據網格,系統會出現問題... – zero51

回答

0

發現!

我真的不解釋爲什麼,但如果我將DataGrid的IsEnabled屬性設置爲false之前更改DataTemplate我沒有任何異常。

因此,我只是簡單地將一個綁定到DataGrid的IsEnabled,並在之前更改爲不同的ControlTemplate,我將該屬性設置爲false,然後在該命令的回調結束時將其設置爲true。

L.