Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

DataGrid: Farbe einstellen einer Zeile wenn kein Fokus auf dem Datagrid ist

22.05.2019 (👁163)

WPF: Selectierte Zeile ohne Fokus

Wie kann man die Farbe bei einen Datagrid einstellen, wenn es keinen Fokus hat

Lösung:

Man muss die Highlight Colors setzen anstatt statischen Farben

<DataGrid.CellStyle>

                <Style TargetType="DataGridCell">

                    <Style.Triggers>

                        <Trigger Property="IsSelected" Value="True">

                            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>

                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>

                            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>

                        </Trigger>

                    </Style.Triggers>

                </Style>

            </DataGrid.CellStyle>

 

 

Im kompletten Code Beispiel xaml wpf datagrid

<DataGrid x:Name="ctlDataGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"

                 AutoGenerateColumns="False"

                 GridLinesVisibility="All"

                 Background="#F0f0f0" 

                 SelectionMode="Single" 

                 IsReadOnly="True" 

                 MouseDoubleClick="ctlDataGrid_MouseDoubleClick"

                  SelectionChanged="CtlDataGrid_SelectionChanged"

                  HeadersVisibility="All"                  

                  Grid.Row="1" 

                  SelectionUnit="FullRow"                   

                 >

            <DataGrid.CellStyle>

                <Style TargetType="DataGridCell">

                    <Style.Triggers>

                        <Trigger Property="IsSelected" Value="True">

                            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>

                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>

                            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>

                        </Trigger>

                    </Style.Triggers>

                </Style>

            </DataGrid.CellStyle>

            

            <DataGrid.Columns>

                <DataGridTextColumn Header="ID" Binding="{Binding IDFeiertag}"/>

                <DataGridTextColumn Header="Datum" Binding="{Binding Datum_Feiertag}" Width="90"/>

                <DataGridTextColumn Header="Feiertag" Binding="{Binding Feiertag}" Width="140"/>

                <DataGridTextColumn Header="Kurz" Binding="{Binding Kurz}">

                    <DataGridTextColumn.CellStyle>

                        <Style TargetType="DataGridCell">

                            <Setter Property="Background" Value="{Binding Farbe}" />

                            <Setter Property="Foreground" Value="{Binding Textfarbe}" />

                        </Style>

                    </DataGridTextColumn.CellStyle>

                </DataGridTextColumn>

 

 

            </DataGrid.Columns>

        </DataGrid>