2012-06-25 38 views
3

我有一種風格是其中包含綁定到一個DynamicResource的XAML我的窗口中創建:DynamicResource結合顏色不工作

<Window.Resources> 
    <local:RowColorConverter x:Key="RowColorConverter" /> 
     <Style x:Key="OddEvenRowStyle"> 
      <Setter Property="DataGridRow.Background"> 
       <Setter.Value> 
        <Binding RelativeSource="{RelativeSource AncestorType=GroupItem}" Path="(ItemsControl.AlternationIndex)" Converter="{StaticResource RowColorConverter}"> 
         <Binding.ConverterParameter> 
          <x:Array Type="Brush"> 
           <SolidColorBrush Color="{DynamicResource RowPrimaryBrush}" /> 
           <SolidColorBrush Color="{DynamicResource RowSecondaryBrush}" /> 
          </x:Array> 
         </Binding.ConverterParameter> 
        </Binding> 
       </Setter.Value> 
      </Setter> 
     </Style> 
</Window.Resources> 

我則樣式分配給RowStyle爲DataGrid:

<DataGrid Name="dataGrid" AutoGenerateColumns="False" Height="Auto" Width="Auto" ItemsSource="{Binding}" RowStyle="{StaticResource OddEvenRowStyle}"> 

在我窗口的初始化我指定這些DynamicResource值:

Resources["RowPrimaryBrush"] = Colors.LightGray; 
Resources["RowSecondaryBrush"] = Colors.DarkGray; 

然而,當我打開的窗口顏色不正常工作:

enter image description here

我敢肯定我的代碼的其餘部分是可以的,因爲當我改變在XAML色值的色彩值:

<x:Array Type="Brush"> 
    <SolidColorBrush Color="LightGray" /> 
    <SolidColorBrush Color="DarkGray" /> 
</x:Array> 

顏色得到正確分配:

enter image description here

這就是爲什麼我導致相信這是與綁定有關的事情。我綁定我的顏色的方式有什麼問題嗎?

+0

您需要做的第一件事是打開數據綁定的調試消息:http://i.stack.imgur.com/MF8i5.png接下來,重新運行並檢查輸出窗口,看看有哪些錯誤。可能還想打開標記和資源字典的跟蹤設置。另外,真正好的問題。歡迎登機。 – Will

+0

@Will我收到的消息是'System.Windows.Data錯誤:4:無法找到與參考綁定的源'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.GroupItem',AncestorLevel ='1''。 BindingExpression:路徑=(0);的DataItem = NULL;目標元素是'DataGridRow'(Name ='');目標屬性是'背景'(類型'刷')' – flamebaud

+0

不幸的是,我不認爲這是問題。你會得到很多這樣的誤報(「DataItem = null」)。順便說一句,你有沒有嘗試用靜態替換動態資源,就像你用實際顏色替換動態資源一樣?只是另一件嘗試。 – Will

回答

2

確保在窗口初始化之前設置好資源。

public MainWindow() 
{ 
    Resources["RowPrimaryBrush"] = Colors.LightGray; 
    Resources["RowSecondaryBrush"] = Colors.DarkGray; 
    InitializeComponent(); 
} 

動態資源在更改時不會更新,如綁定。它只是推遲到運行時,而不是在編譯時進行評估。


不熟悉這個限制,但並不奇怪。您可能需要重寫RowColorConverter直接採取它的參數,然後通過

(Resources["RowColorConverter"] as RowColorConverter).Parameter = 
    new Brush[] 
    { 
     Brushes.LightGray, 
     Brushes.DarkGray 
    } 

從隱藏代碼更新或您的RowColorConverter

#region Brushes Attached DependencyProperty 
public static readonly DependencyProperty BrushesProperty = DependencyProperty.RegisterAttached(
    BrushesPropertyName, 
    typeof(SolidColorBrush[]), 
    typeof(RowColorConverter), 
    new FrameworkPropertyMetadata(null) 
); 
public const string BrushesPropertyName = "Brushes"; 
public static void SetBrushes(DependencyObject element, SolidColorBrush[] value) 
{ 
    element.SetValue(BrushesProperty, value); 
} 
public static SolidColorBrush[] GetBrushes(DependencyObject element) 
{ 
    return (SolidColorBrush[])element.GetValue(BrushesProperty); 
} 
#endregion 

中定義的附加屬性,你可以設置不同的每個網格

<DataGrid Name="dataGrid" SnipPointlessAttributes="True"> 
    <local:RowColorConverter.Colors> 
     <x:Array Type="Brush"> 
      <SolidColorBrush Color="LightGray" /> 
      <SolidColorBrush Color="DarkGray" /> 
     </x:Array> 
    </local:RowColorConverter.Colors> 
</DataGrid> 
+0

對不起,應該表明我已經這樣做了。 – flamebaud

2

Binding.ConverterParameter不是WPF邏輯樹的一部分,因此在其中執行動態資源查找將不起作用。

+1

我可以理解這可能會導致問題,但是您是否有解決將動態畫筆傳遞給我的轉換器的困難的建議? – flamebaud

0

使用MultiBinding代替與轉換器參數的綁定。