2015-11-11 97 views
0

我已經創建了與已經用於其內部運作定義的資源一個DataGrid一個用戶控件:現在如何在已有資源的WPF組件上設置XAML中的資源?

 <DataGrid.Resources> 
      <Style TargetType="{x:Type DataGridColumnHeader}"> 
       <Setter Property="ContentTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <Grid> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="23" /> 
            <ColumnDefinition Width="*" /> 
           </Grid.ColumnDefinitions> 
           <Button x:Name="ExcelFilterButton" 
             Grid.Column="0" 
             Margin="0,0,0,0" 
             BorderThickness="0" 
             Click="ExcelFilterButton_Click" 
             Focusable="False" 
             Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 
             Tag="{Binding}"> 
            <Image Width="19" 
              Height="19" 
              Source="Resources\NoSortNoFilter.png" 
              Tag="{Binding}" /> 
           </Button> 
           <TextBlock Grid.Column="1" 
              HorizontalAlignment="Center" 
              VerticalAlignment="Center" 
              Text="{Binding}" /> 
          </Grid> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </DataGrid.Resources> 

,而試圖在應用程序中使用的組件,並與下面的XAML

      <adg:AdvancedSortFilterDataGrid.Resources> 
           <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}" /> 
           <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="{x:Static SystemColors.HighlightTextColor}" />         
          </adg:AdvancedSortFilterDataGrid.Resources> 
設置一些顏色

獲取我下面的運行時錯誤:

'Set property 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.' Line number '200' and line position '34'. 

我試圖用資源屬性的組件,只需添加資源到現有資源,但獲得相同的錯誤。這裏的屬性:

Public Shadows Property Resources As ResourceDictionary 
    Get 
     Return dataGrid.Resources 
    End Get 
    Set(value As ResourceDictionary) 
     For Each Key As Object In value.Keys 
      dataGrid.Resources.Add(Key, value(Key)) 
     Next 
    End Set 
End Property 

希望有人能讓我直接。

編輯:的InnerException:

Cannot re-initialize ResourceDictionary instance. 

回答

0

好吧,看來我發現StackOverflow的答案。不知道爲什麼我在最初的搜索中沒有找到這些。無論如何,我發現如果我將資源放在組件和使用應用程序中的ResourcesDictionary元素中,並將ResourcesDictionary元素放入組件中的MergedDictionaries元素內,它就可以正常工作。

組件:

 <DataGrid.Resources> 
      <ResourceDictionary> 
       <ResourceDictionary.MergedDictionaries> 
        <ResourceDictionary> 
         <Style TargetType="{x:Type DataGridColumnHeader}"> 
          <Setter Property="ContentTemplate"> 
           <Setter.Value> 
            <DataTemplate> 
             <Grid> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="23" /> 
               <ColumnDefinition Width="*" /> 
              </Grid.ColumnDefinitions> 
              <Button x:Name="ExcelFilterButton" 
                Grid.Column="0" 
                Margin="0,0,0,0" 
                BorderThickness="0" 
                Click="ExcelFilterButton_Click" 
                Focusable="False" 
                Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 
                Tag="{Binding}"> 
               <Image Width="19" 
                 Height="19" 
                 Source="Resources\NoSortNoFilter.png" 
                 Tag="{Binding}" /> 
              </Button> 
              <TextBlock Grid.Column="1" 
                 HorizontalAlignment="Center" 
                 VerticalAlignment="Center" 
                 Text="{Binding}" /> 
             </Grid> 
            </DataTemplate> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </ResourceDictionary> 
       </ResourceDictionary.MergedDictionaries> 
      </ResourceDictionary> 
     </DataGrid.Resources> 

使用應用程序:

      <adg:AdvancedSortFilterDataGrid.Resources> 
           <ResourceDictionary> 
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}" /> 
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="{x:Static SystemColors.HighlightTextColor}" /> 
           </ResourceDictionary> 
          </adg:AdvancedSortFilterDataGrid.Resources> 

我改變了資源屬性的組件的新ResourcesDictionary添加到MergedDictionaries。

Public Shadows Property Resources As ResourceDictionary 
    Get 
     If dataGrid.Resources.MergedDictionaries.Count < 2 Then 
      Return Nothing 
     Else 
      Return dataGrid.Resources.MergedDictionaries(1) 
     End If 
    End Get 
    Set(value As ResourceDictionary) 
     Do Until dataGrid.Resources.MergedDictionaries.Count = 1 
      dataGrid.Resources.MergedDictionaries.RemoveAt(1) 
     Loop 
     dataGrid.Resources.MergedDictionaries.Add(value) 
    End Set 
End Property 

現在它工作正常。這太簡單了,看不到。