C# – silverlight datagrid – binding each row’s style

csilverlightxaml

i've got a silverlight (v2) datagrid where some items are section headers and as such must appear with a different background colour.

i'm trying to do this with the following xaml:

        <dg:DataGrid.RowStyle>
            <Style TargetType="dg:DataGridRow">
                <Setter Property="Background" Value="{Binding Path=Background, Mode=OneTime}" />
            </Style>
        </dg:DataGrid.RowStyle>

i expect it to bind the Background property of the datagrid row viewmodel to each row's Background property, instead i get a lovely unknown xaml parsing error:

{System.Windows.Markup.XamlParseException: AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR [Line: 16 Position: 57]
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at Etana.Survey.Silverlight.UserInterface.Views.MaximumProbableLossPage.InitializeComponent()
   at Etana.Survey.Silverlight.UserInterface.Views.MaximumProbableLossPage..ctor()}

if i try to explicitly specify "Red" and not try and bind the style, then it works, so I wonder if silverlight would allow me to bind a style like that or if there's some other trick to it.

(the xaml is based on a wpf implementation of this which works fine)

any input would be much appreciated

Best Answer

Change your binding to TemplateBinding. eg

<dg:DataGrid.RowStyle>
            <Style TargetType="dg:DataGridRow">
                <Setter Property="Background" Value="{TemplateBinding Background, Mode=OneTime}" />
            </Style>
</dg:DataGrid.RowStyle>
Related Topic