我有一種風格是其中包含綁定到一個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;
然而,當我打開的窗口顏色不正常工作:
我敢肯定我的代碼的其餘部分是可以的,因爲當我改變在XAML色值的色彩值:
<x:Array Type="Brush">
<SolidColorBrush Color="LightGray" />
<SolidColorBrush Color="DarkGray" />
</x:Array>
顏色得到正確分配:
這就是爲什麼我導致相信這是與綁定有關的事情。我綁定我的顏色的方式有什麼問題嗎?
您需要做的第一件事是打開數據綁定的調試消息:http://i.stack.imgur.com/MF8i5.png接下來,重新運行並檢查輸出窗口,看看有哪些錯誤。可能還想打開標記和資源字典的跟蹤設置。另外,真正好的問題。歡迎登機。 – Will
@Will我收到的消息是'System.Windows.Data錯誤:4:無法找到與參考綁定的源'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.GroupItem',AncestorLevel ='1''。 BindingExpression:路徑=(0);的DataItem = NULL;目標元素是'DataGridRow'(Name ='');目標屬性是'背景'(類型'刷')' – flamebaud
不幸的是,我不認爲這是問題。你會得到很多這樣的誤報(「DataItem = null」)。順便說一句,你有沒有嘗試用靜態替換動態資源,就像你用實際顏色替換動態資源一樣?只是另一件嘗試。 – Will