TRS
TRS

Reputation: 2097

Style a cell in GridView MVVM

I have this cell template for my gridView

   <GridViewColumn.CellTemplate>
     <DataTemplate>
        <Border Style="{StaticResource ShadowBorderStyle}"  Height="75" Width="100">
          <TextBox Text="{Binding MyText}" />
         </Border>
     </DataTemplate>
   </GridViewColumn.CellTemplate>

How can I style the cell so that when mouse is pressed on cell,it should show red border over it.Is it possible to do for all the cells in GridView as well. I am using MVVM so I dont want to do it code behind.

Upvotes: 0

Views: 356

Answers (2)

Nitin Purohit
Nitin Purohit

Reputation: 18578

Putting on the border of textbox in cell template:

<Border.Style>
    <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <Trigger Property="IsMouseCaptureWithin" Value="True">
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="BorderThickness" Value="2"/>
                </Trigger>
            </Style.Triggers>
        </Style>
</Border.Style>

see if this helps you.

Thanks

Upvotes: 0

eran otzap
eran otzap

Reputation: 12533

  <DataGrid>
      <DataGrid.Resources>
         <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                  <EventTrigger RoutedEvent="PreviewMouseLeftButtonDown">
                     <BeginStoryboard>
                          <Storyboard>
                             <ColorAnimation
                                    Storyboard.TargetProperty="(Border.BorderBrush).(Color)"
                                    To="Red"
                                    Duration="0:0:0" />
                          </Storyboard>
                      </BeginStoryboard>                    
                  </EventTrigger>                
              </Style.Triggers>            
          </Style>
      </DataGrid.Resources>
 </DataGrid>

Upvotes: 1

Related Questions