5

我想找到一種方法來綁定DataGrid的行的背景顏色到我的綁定對象的屬性。Silverlight的DataGrid行顏色綁定

這是我的XAML:

<sdk:DataGrid ItemsSource="{Binding MyItems}" /> 

我使用的MVVM光工具包使用Silverlight 4

回答

8

您可以通過更改的行模板,做到這一點:

<sdk:DataGrid ItemsSource="{Binding MyItems}"> 
    <sdk:DataGrid.RowStyle> 
     <Style TargetType="sdk:DataGridRow"> 
      <Setter Property="Template"> 
      ... 
       <Rectangle x:Name="BackgroundRectangle" Fill="{Binding ColorPropertyOfItem}" /> 
      ... 
      </Setter> 
     </Style> 
    </sdk:DataGrid.RowStyle> 
</sdk:DataGrid> 

或者您可以將每列標記爲TemplateColumn並在其中明確設置背景顏色:

<sdk:DataGridTemplateColumn Header="ColumnName" SortMemberPath="ColumnProperty"> 
    <sdk:DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding ColumnProperty}" Background="{Binding ColorPropertyOfItem}" /> 
     </DataTemplate> 
    </sdk:DataGridTemplateColumn.CellTemplate> 
</sdk:DataGridTemplateColumn> 

這裏是DataGridRow類的完整模板:

<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="local:DataGridRow" 
      xmlns:localprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data" 
      xmlns:local="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
      xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"> 
      <localprimitives:DataGridFrozenGrid Name="Root"> 
       <vsm:VisualStateManager.VisualStateGroups> 
        <vsm:VisualStateGroup x:Name="CommonStates"> 
         <vsm:VisualState x:Name="Normal"/> 
        <vsm:VisualState x:Name="NormalAlternatingRow"> 
          <Storyboard> 
           <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="0"/> 
          </Storyboard> 
         </vsm:VisualState> 
         <vsm:VisualState x:Name="MouseOver"> 
          <Storyboard> 
           <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To=".5"/> 
          </Storyboard> 
         </vsm:VisualState> 
         <vsm:VisualState x:Name="NormalSelected"> 
          <Storyboard> 
           <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> 
          </Storyboard> 
         </vsm:VisualState> 
         <vsm:VisualState x:Name="MouseOverSelected"> 
          <Storyboard> 
           <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> 
          </Storyboard> 
         </vsm:VisualState> 
         <vsm:VisualState x:Name="UnfocusedSelected"> 
          <Storyboard> 
           <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> 
           <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Fill).Color" To="#FFE1E7EC"/> 
          </Storyboard> 
         </vsm:VisualState> 
        </vsm:VisualStateGroup> 
        <vsm:VisualStateGroup x:Name="ValidationStates"> 
         <vsm:VisualState x:Name="Valid"/> 
         <vsm:VisualState x:Name="Invalid"> 
          <Storyboard> 
           <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Visibility"> 
            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/> 
           </ObjectAnimationUsingKeyFrames> 
           <DoubleAnimation Storyboard.TargetName="InvalidVisualElement" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> 
          </Storyboard> 
         </vsm:VisualState> 
        </vsm:VisualStateGroup> 
       </vsm:VisualStateManager.VisualStateGroups> 
       <Grid.RowDefinitions> 
        <RowDefinition/> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition/> 
       </Grid.ColumnDefinitions> 

       <Grid.Resources> 
        <Storyboard x:Key="DetailsVisibleTransition"> 
         <DoubleAnimation Storyboard.TargetName="DetailsPresenter" Storyboard.TargetProperty="ContentHeight" Duration="00:00:0.1" /> 
        </Storyboard> 
       </Grid.Resources> 

       <Rectangle x:Name="BackgroundRectangle" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="{Binding ColorPropertyOfItem}"/> 
       <Rectangle x:Name="InvalidVisualElement" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="#FFF7D8DB"/> 

       <localprimitives:DataGridRowHeader Grid.RowSpan="3" Name="RowHeader" localprimitives:DataGridFrozenGrid.IsFrozen="True" /> 
       <localprimitives:DataGridCellsPresenter Grid.Column="1" Name="CellsPresenter" localprimitives:DataGridFrozenGrid.IsFrozen="True" /> 
       <localprimitives:DataGridDetailsPresenter Grid.Row="1" Grid.Column="1" Name="DetailsPresenter" /> 
       <Rectangle Grid.Row="2" Grid.Column="1" Name="BottomGridLine" HorizontalAlignment="Stretch" Height="1" /> 
      </localprimitives:DataGridFrozenGrid> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 

而且不要忘記更改Binding ColorPropertyOfItem到模型中的類型Brush的不動產。

+0

感謝您的回答,我使用了第一個解決方案,因爲我的DataGrid將有很多列,並且每次創建TemplateColumn都會太冗長)。 – 2010-11-24 16:18:31