2013-07-14 4 views
0

У меня есть переопределения DataGrid. Ниже мой код и XAML. Как установить стиль для изменения цвета фона DataGridCell при выборе ячейки?WPF переопределил datagrid как установить DatagridCell Выбранный стиль

<vw:DataGridExt 
    Grid.Row="1" 
    AlternatingRowBackground="LightGray" 
    ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Value,Mode=OneWay, IsAsync=True}" 
    Background="White" 
    AutoGenerateColumns="True" 
    IsReadOnly="True" 
    CanUserResizeRows="False" 
    ClipboardCopyMode="IncludeHeader" 
    CanUserSortColumns="True" 
    CanUserAddRows="False" 
    SelectionMode="Extended"/>     

public class DataGridExt : DataGrid 
{ 
    public DataGridExt() 
    { 
     this.AutoGeneratedColumns += new EventHandler(DataGrid_AutoGeneratedColumns); 
    } 
} 
+0

Добавить в мой код xaml. Jack

ответ

0

Вы должны обеспечить стиль для DataGridCell

<Style TargetType="{x:Type DataGridCell}"> 
    <Setter Property="Background" Value="Transparent" />   
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="BorderBrush" Value="Transparent"/>  
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type DataGridCell}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" > 
        <ContentPresenter /> 
       </Border>        
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
      <Setter Property="BorderBrush" Value="Black" /> 
     </Trigger> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="Background" Value="Gray"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

и установить

SelectionUnit="Cell" for your DataGrid. 

Надеются, что это помогает.

+0

он работает как я хочу. я пропустил часть SelectionUnit = "Cell" – Jack

Смежные вопросы