2014-11-21 172 views
1

請原諒我我只是一個新手WPF設置屬性System.Windows.FrameworkElement.Style拋出異常

當我試圖在VS2012中調試我的小WPF應用程序時,我收到了一個異常,請查看下面的截圖。我試圖找出究竟是什麼例外。但沒有找到一種方法來獲得更多的細節信息的例外。因爲代碼似乎在XAML的第一行中斷了。

enter image description here enter image description here

我想它可以通過在我的風格DataGridDemoStyle惡意代碼造成的。但我不知道什麼代碼會導致錯誤。有沒有辦法看到錯誤的細節如InnerException

謝謝。

更新

後我檢查由線樣式代碼行。

我發現一個名爲DataGridDemoRowStyle的樣式導致了這個錯誤。我不知道爲什麼這個樣式會導致錯誤。因爲如果我刪除它。錯誤將消失。請在下面查看。

<Style x:Key="DataGridDemoStyle" TargetType="{x:Type DataGrid}"> 
     <!--<Setter Property="AlternatingRowBackground" Value="{StaticResource RowBackgroundAlternateBrush}" />--> 
     <!--<Setter Property="BorderBrush" Value="#FF688CAF"/>--> 
     <!--<Setter Property="Background" Value="{DynamicResource bearBrush}" />--> 
     <!--<Setter Property="ColumnHeaderHeight" Value="50" />--> 
     <!--<Setter Property="HeadersVisibility" Value="All" />--> 
     <!--<Setter Property="RowBackground" Value="{StaticResource RowBackgroundBrush}" />--> 
     <!--<Setter Property="AlternationCount" Value="4" />--> 
     <Setter Property="RowStyle" Value="{StaticResource DataGridDemoRowStyle}" /> 

     <!--<Setter Property="RowHeaderWidth" Value="50" />--> 
     <Setter Property="RowHeight" Value="22" /> 
     <Setter Property="HorizontalGridLinesBrush" Value="{StaticResource DataGridHorizontalLinesBrush}" /> 
     <Setter Property="CellStyle" Value="{StaticResource DataGridCellStyle}" /><!----> 

     <Setter Property="ColumnHeaderStyle" Value="{StaticResource DatagridColumnHeaderCustomTemplateStyle}" /> 
    </Style> 
    <!--I don't know why below style will cause the error. If I remove it . the error will gone.--> 
    <Style x:Key="DataGridDemoRowStyle" TargetType="{x:Type DataGridRow}"> 
     <Style.Triggers> 
      <Trigger Property="AlternationIndex" Value="2" > 
       <Setter Property="Background" Value="{StaticResource RowBackgroundAlternationIndex2Brush}" /> 
      </Trigger> 
      <Trigger Property="AlternationIndex" Value="3"> 
       <Setter Property="Background" Value="{StaticResource RowBackgroundAlternationIndex3Brush}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

DataGridDemoRowStyle風格有什麼問題嗎?謝謝。

回答

2

我找出原因。

如果我把子風格放在主風格DataGridDemoStyle之後。 WPF將無法找到並加載它們。所以我必須把所有的依賴風格放在DataGridDemoStyle之前。

正確的做法:

<Style x:Key="substyle1" >..</Style> 
<Style x:Key="substyle2" >..</Style> 
.. 
<Style x:Key="substylen" >..</Style> 
<Style x:Key="mainstyle" > 
    ... 
    <Setter Property="xxx" Value="{StaticResource substylen}" /> 
</style> 

PS:似乎由線爲XAML遵守行。應該在指定的代碼編譯行之前準備好所有的依賴關係。我不知道這是否正確。請協助審查。

謝謝。

+0

你是對的。訂單很重要。 – 2014-11-21 08:41:52

+0

感謝您的幫助來審查我的問題。祝你有美好的一天。:) – 2014-11-21 08:42:32

+0

沒問題。 +1。 – 2014-11-21 08:43:12