none
DataGridRow Style Ändern RRS feed

  • Frage

  • Hi Leute,

     

    wie kann ich den Style Ändern wenn ich in einem Data Grid mit den Cursertasten navigiere ?

    D.h Ich habe ein DataGrid und in diesem bewege ich mich mit den Pfeiltasten und die Aktuelle Celle auf der der Curser steht soll ene andere Farbe bekommen ?

     

    Danke und Grüße

    Klaus

     

    Freitag, 27. August 2010 08:06

Antworten

  • Hallo Klaus,

    eine Möglichkeit zum Beispiel:

     DataGridCell letzteZelle;
     Brush backBrush = new SolidColorBrush(Colors.Yellow);
     Brush foreBrush = new SolidColorBrush(Colors.Black);
     Brush defBackBrush = new SolidColorBrush(Colors.White);
     Brush defForeBrush = new SolidColorBrush(Colors.Black);
    
     void Dgv_CurrentCellChanged(object sender, EventArgs e)
     {
     DataGridColumn col = dgv.CurrentCell.Column;
     var presenter = col.GetCellContent(dgv.CurrentCell.Item);
     FrameworkElement fe = GetParent(presenter, typeof(DataGridCell));
     if (fe == null) return;
     DataGridCell cell = fe as DataGridCell;
     if (cell == null) return;
     if (letzteZelle != null)
     {
     letzteZelle.Background = defBackBrush;
     letzteZelle.Foreground = defForeBrush;
     }
     cell.Background = backBrush; cell.Foreground = foreBrush;
     letzteZelle = cell;
     }
    
     private FrameworkElement GetParent(FrameworkElement child, Type targetType)
     {
     object parent = child.Parent;
     if (parent == null) return null;
     if (parent.GetType() == targetType)
     return (FrameworkElement)parent;
     else
     return GetParent((FrameworkElement)parent, targetType);
     }
    

     


    Mit folgendem wird zum Beispiel die Row selektiert:

     <Window.Resources>
     <Style TargetType="{x:Type DataGridCell}">
      <Style.Triggers>
      <Trigger Property="DataGridCell.IsSelected" Value="True">
       <Setter Property="Background" Value="Yellow" />
      </Trigger>
      </Style.Triggers>
     </Style>
     </Window.Resources>



    und pro Zellen-KeyboardFocus nur eine Zelle zum Beispiel über Style:

     <Window.Resources>
      <Style TargetType="{x:Type DataGridCell}">
       <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
         <Setter Property="Background" Value="White"/>
         <Setter Property="Foreground" Value="Black"/>
         <Setter Property="BorderBrush" Value="LightGray"/>
        </Trigger>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
         <Setter Property="Background" Value="Yellow"/>
         <Setter Property="Foreground" Value="Black"/>
         <Setter Property="BorderBrush" Value="Orange"/>
        </Trigger>
       </Style.Triggers>
      </Style>
     </Window.Resources>

    ciao Frank
    Freitag, 27. August 2010 15:11
  • Hallo Klaus,

    ein mögliche Lösung Deines Styles zum Beispiel so:

     <Window.Resources>
    
      <Style TargetType="{x:Type DataGridCell}" >
       <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
         <Setter Property="Background" Value="White"/>
         <Setter Property="Foreground" Value="Black"/>
         <Setter Property="BorderBrush" Value="LightGray"/>
        </Trigger>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
         <Setter Property="Background" Value="Yellow"/>
         <Setter Property="Foreground" Value="Black"/>
         <Setter Property="BorderBrush" Value="Orange"/>
        </Trigger>
       </Style.Triggers>
    
       <Setter Property="Template">
        <Setter.Value>
         <ControlTemplate TargetType="{x:Type DataGridCell}">
          <Grid Background="{TemplateBinding Background}">
           <ContentPresenter 
            HorizontalAlignment="Right" 
            VerticalAlignment="Center"/>
          </Grid>
         </ControlTemplate>
        </Setter.Value>
       </Setter>
      </Style>
    
      <Style TargetType="{x:Type DataGridColumnHeader}">
       <Setter Property="HorizontalContentAlignment" Value="Right"/>
      </Style><br/>
     </Window.Resources><br/>
    

    ______________________

    oder so:

     <Window.Resources>
      <Style TargetType="{x:Type DataGridCell}" >
       <Setter Property="Template">
        <Setter.Value>
         <ControlTemplate TargetType="{x:Type DataGridCell}">
          <ControlTemplate.Triggers>
           <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="White"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="BorderBrush" Value="LightGray"/>
           </Trigger>
           <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="Background" Value="Yellow"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="BorderBrush" Value="Orange"/>
           </Trigger>
          </ControlTemplate.Triggers>
          <Grid Background="{TemplateBinding Background}">
           <ContentPresenter 
            HorizontalAlignment="Right" 
            VerticalAlignment="Center"/>
          </Grid>
         </ControlTemplate>
        </Setter.Value>
       </Setter>
      </Style>
    
      <Style TargetType="{x:Type DataGridColumnHeader}">
       <Setter Property="HorizontalContentAlignment" Value="Right"/>
      </Style>
    
     </Window.Resources>
    

    ciao Frank
    Montag, 30. August 2010 18:41

Alle Antworten

  • Hallo Klaus,

    eine Möglichkeit zum Beispiel:

     DataGridCell letzteZelle;
     Brush backBrush = new SolidColorBrush(Colors.Yellow);
     Brush foreBrush = new SolidColorBrush(Colors.Black);
     Brush defBackBrush = new SolidColorBrush(Colors.White);
     Brush defForeBrush = new SolidColorBrush(Colors.Black);
    
     void Dgv_CurrentCellChanged(object sender, EventArgs e)
     {
     DataGridColumn col = dgv.CurrentCell.Column;
     var presenter = col.GetCellContent(dgv.CurrentCell.Item);
     FrameworkElement fe = GetParent(presenter, typeof(DataGridCell));
     if (fe == null) return;
     DataGridCell cell = fe as DataGridCell;
     if (cell == null) return;
     if (letzteZelle != null)
     {
     letzteZelle.Background = defBackBrush;
     letzteZelle.Foreground = defForeBrush;
     }
     cell.Background = backBrush; cell.Foreground = foreBrush;
     letzteZelle = cell;
     }
    
     private FrameworkElement GetParent(FrameworkElement child, Type targetType)
     {
     object parent = child.Parent;
     if (parent == null) return null;
     if (parent.GetType() == targetType)
     return (FrameworkElement)parent;
     else
     return GetParent((FrameworkElement)parent, targetType);
     }
    

     


    Mit folgendem wird zum Beispiel die Row selektiert:

     <Window.Resources>
     <Style TargetType="{x:Type DataGridCell}">
      <Style.Triggers>
      <Trigger Property="DataGridCell.IsSelected" Value="True">
       <Setter Property="Background" Value="Yellow" />
      </Trigger>
      </Style.Triggers>
     </Style>
     </Window.Resources>



    und pro Zellen-KeyboardFocus nur eine Zelle zum Beispiel über Style:

     <Window.Resources>
      <Style TargetType="{x:Type DataGridCell}">
       <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
         <Setter Property="Background" Value="White"/>
         <Setter Property="Foreground" Value="Black"/>
         <Setter Property="BorderBrush" Value="LightGray"/>
        </Trigger>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
         <Setter Property="Background" Value="Yellow"/>
         <Setter Property="Foreground" Value="Black"/>
         <Setter Property="BorderBrush" Value="Orange"/>
        </Trigger>
       </Style.Triggers>
      </Style>
     </Window.Resources>

    ciao Frank
    Freitag, 27. August 2010 15:11
  • Hi Frank,

     

    danke für die Antwort. Es Funktioniert auch.

    Nur :-)

    wenn ich folgendes Code habe.

     <Style x:Key="RightAlignedCellStyle" TargetType="{x:Type DataGridCell}">
          <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
              <Grid Background="Transparent">
                <ContentPresenter HorizontalAlignment="Right"
                         VerticalAlignment="Center"/>
             </Grid>
              <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="True">
                  <Setter Property="Background" Value="Yellow"/>
                  <Setter Property="Foreground" Value="Black"/>
                  <Setter Property="BorderBrush" Value="Orange"/>
                </Trigger>
    
              </ControlTemplate.Triggers>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    

    Feuert meinTrigger nicht ?  Warum nicht ?

     

    Grüße Klaus

    Montag, 30. August 2010 09:07
  • Hallo Klaus,

    ein mögliche Lösung Deines Styles zum Beispiel so:

     <Window.Resources>
    
      <Style TargetType="{x:Type DataGridCell}" >
       <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
         <Setter Property="Background" Value="White"/>
         <Setter Property="Foreground" Value="Black"/>
         <Setter Property="BorderBrush" Value="LightGray"/>
        </Trigger>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
         <Setter Property="Background" Value="Yellow"/>
         <Setter Property="Foreground" Value="Black"/>
         <Setter Property="BorderBrush" Value="Orange"/>
        </Trigger>
       </Style.Triggers>
    
       <Setter Property="Template">
        <Setter.Value>
         <ControlTemplate TargetType="{x:Type DataGridCell}">
          <Grid Background="{TemplateBinding Background}">
           <ContentPresenter 
            HorizontalAlignment="Right" 
            VerticalAlignment="Center"/>
          </Grid>
         </ControlTemplate>
        </Setter.Value>
       </Setter>
      </Style>
    
      <Style TargetType="{x:Type DataGridColumnHeader}">
       <Setter Property="HorizontalContentAlignment" Value="Right"/>
      </Style><br/>
     </Window.Resources><br/>
    

    ______________________

    oder so:

     <Window.Resources>
      <Style TargetType="{x:Type DataGridCell}" >
       <Setter Property="Template">
        <Setter.Value>
         <ControlTemplate TargetType="{x:Type DataGridCell}">
          <ControlTemplate.Triggers>
           <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="White"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="BorderBrush" Value="LightGray"/>
           </Trigger>
           <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="Background" Value="Yellow"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="BorderBrush" Value="Orange"/>
           </Trigger>
          </ControlTemplate.Triggers>
          <Grid Background="{TemplateBinding Background}">
           <ContentPresenter 
            HorizontalAlignment="Right" 
            VerticalAlignment="Center"/>
          </Grid>
         </ControlTemplate>
        </Setter.Value>
       </Setter>
      </Style>
    
      <Style TargetType="{x:Type DataGridColumnHeader}">
       <Setter Property="HorizontalContentAlignment" Value="Right"/>
      </Style>
    
     </Window.Resources>
    

    ciao Frank
    Montag, 30. August 2010 18:41