Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF数据网格样式_C#_Wpf_Datagrid - Fatal编程技术网

C# WPF数据网格样式

C# WPF数据网格样式,c#,wpf,datagrid,C#,Wpf,Datagrid,我有一个具有以下样式的DataGrid <Style x:Key="DataGridRowStyle1" TargetType="{x:Type DataGridRow}"> <Setter Property="Foreground" Value="#FFB3B3B3"/> <Setter Property="Height" Value="2

我有一个具有以下样式的DataGrid

<Style x:Key="DataGridRowStyle1" TargetType="{x:Type DataGridRow}">
    <Setter Property="Foreground" Value="#FFB3B3B3"/>
    <Setter Property="Height" Value="25"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="Template" Value="{DynamicResource DataGridRowControlTemplate1}"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="#FF262626"/>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="#FF383838"/>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#FF333333"/>
        </Trigger>
    </Style.Triggers>
</Style>

看起来是这样的:

<Window.Resources>
    <SolidColorBrush x:Key="GotFocusColor" Color="Green" />
    <SolidColorBrush x:Key="LostFocusColor" Color="Transparent" />

    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Foreground" Value="#FFB3B3B3"/>
        <Setter Property="Height" Value="25"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>

        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="#FF262626"/>
            </Trigger>

            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background" Value="#FF383838"/>
            </Trigger>

            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="#FF333333"/>
            </Trigger>

            <EventTrigger RoutedEvent="DataGrid.GotFocus">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource GotFocusColor}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

            <EventTrigger RoutedEvent="DataGrid.LostFocus">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource LostFocusColor}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

DataGrid失去焦点时出现我的问题:


如何使其外观独立于焦点?

在尝试找到解决方案之前,请查看DataGrid、DataGridRow等的
样式/模板。焦点上的StyleTrigger(
IsFocused
trigger),因为它不是默认行为

如果没有,请尝试为事件添加
EvenTriggers
,如下所示:

<Window.Resources>
    <SolidColorBrush x:Key="GotFocusColor" Color="Green" />
    <SolidColorBrush x:Key="LostFocusColor" Color="Transparent" />

    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Foreground" Value="#FFB3B3B3"/>
        <Setter Property="Height" Value="25"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>

        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="#FF262626"/>
            </Trigger>

            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background" Value="#FF383838"/>
            </Trigger>

            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="#FF333333"/>
            </Trigger>

            <EventTrigger RoutedEvent="DataGrid.GotFocus">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource GotFocusColor}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

            <EventTrigger RoutedEvent="DataGrid.LostFocus">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource LostFocusColor}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>


团队中的一员应该测试您的解决方案并给我反馈!!很抱歉延迟。@m.samy:我的回答对您有所帮助,或者您有问题吗?感谢您为WPF MUI应用程序中的datagrid所做的这项工作,但刚刚将Windows.Resource更改为UserControl.Resource,因为这是在UserControls中。您可以共享完整的xaml样式吗?看起来不错。我能问一下你用的是哪种字体吗?我不记得了,sorry@DanSewell,它看起来像微软Verdana的狭隘版本Nina。非常好的风格!你能发布xaml样式吗?