WPF:如何使用ShowsPreview

时间:2017-02-15 20:43:45

标签: wpf

我在WPF中创建一个带有深色背景的应用程序,其中包含一个网格分割器。网格分割器必须与ShowPreview = True一起使用。网格分割器的预览“阴影”与我的背景非常融合,所以我想编辑它的样式。我找不到任何关于如何更改预览风格的信息。我希望看到一个可以改变的画笔属性。有什么想法吗?

<GridSplitter
    Width="2"
    Background="{DynamicResource BaseBorderBrush}"
    ResizeBehavior="CurrentAndNext"
    ShowsPreview="True">
</GridSplitter>

1 个答案:

答案 0 :(得分:0)

使用its Style并更改此部分:<Setter Property="Background" Value="Red" />。请注意,我将其更改为红色。

 <GridSplitter
        HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="50" ResizeBehavior="CurrentAndNext" ShowsPreview="True">
        <GridSplitter.Style>
            <Style TargetType="controls:GridSplitter">
                <Setter Property="Background" Value="#FFFFFFFF" />
                <Setter Property="IsTabStop" Value="true" />
                <Setter Property="HorizontalAlignment" Value="Right" />
                <Setter Property="PreviewStyle">
                    <Setter.Value>
                        <Style TargetType="Control">
                            <!-- Background -->
                            <Setter Property="Background" Value="Red" />
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="Control">
                                        <Grid x:Name="Root" Opacity=".5"> 
                                            <Rectangle Fill="{TemplateBinding Background}" />

                                            <!-- Horizontal Template -->
                                            <Grid x:Name="HorizontalTemplate" Height="6">
                                                <!-- Just show the faint gray grid splitter rectangle with no other details -->
                                            </Grid>

                                            <!-- Vertical Template -->
                                            <Grid x:Name="VerticalTemplate" Visibility="Collapsed" Width="6">
                                                <!-- Just show the faint gray grid splitter rectangle with no other details -->
                                            </Grid>

                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="controls:GridSplitter">
                            <Grid x:Name="Root" IsHitTestVisible="{TemplateBinding IsEnabled}">

                                <!-- VSM -->
                                <vsm:VisualStateManager.VisualStateGroups>
                                    <vsm:VisualStateGroup x:Name="CommonStates">
                                        <vsm:VisualState x:Name="Normal" />
                                        <vsm:VisualState x:Name="MouseOver" />
                                        <vsm:VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="0.5" Duration="0" />
                                            </Storyboard>
                                        </vsm:VisualState>
                                    </vsm:VisualStateGroup>
                                    <vsm:VisualStateGroup x:Name="FocusStates">
                                        <vsm:VisualStateGroup.Transitions>
                                            <vsm:VisualTransition GeneratedDuration="0" />
                                        </vsm:VisualStateGroup.Transitions>
                                        <vsm:VisualState x:Name="Unfocused" />
                                        <vsm:VisualState x:Name="Focused">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="FocusVisual" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                            </Storyboard>
                                        </vsm:VisualState>
                                    </vsm:VisualStateGroup>
                                </vsm:VisualStateManager.VisualStateGroups>

                                <!-- Background -->
                                <Rectangle Fill="{TemplateBinding Background}" StrokeThickness="0" />

                                <!-- Horizontal Template -->
                                <Grid x:Name="HorizontalTemplate" Height="10">
                                    <StackPanel Height="6" VerticalAlignment="Center">
                                        <Rectangle Height="1" Margin="1" Width="10" StrokeThickness="0" Fill="#FF868686" />
                                        <Rectangle Height="1" Margin="1" Width="10" StrokeThickness="0" Fill="#FF868686" />
                                    </StackPanel>
                                </Grid>

                                <!-- Vertical Template -->
                                <Grid x:Name="VerticalTemplate" Visibility="Collapsed" Width="10">
                                    <StackPanel Width="6" VerticalAlignment="Center" Orientation="Horizontal">
                                        <Rectangle Width="1" Margin="1" Height="10" StrokeThickness="0" Fill="#FF868686" />
                                        <Rectangle Width="1" Margin="1" Height="10" StrokeThickness="0" Fill="#FF868686" />
                                    </StackPanel>
                                </Grid>

                                <!-- Focus Visual -->
                                <Rectangle x:Name="FocusVisual" Stroke="#FF6DBDD1" StrokeThickness="1" Opacity="0" IsHitTestVisible="false" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GridSplitter.Style>
    </GridSplitter>

不要忘记Xaml中的XML命名空间映射:

xmlns:vsm="clr-namespace:System.Windows;assembly=PresentationFramework"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"