WPF 动态加载xaml文件,仿win10滚动条样式

win10滚动条,就是鼠标不在最右边时候为滚动标志,移动到滚动位置改变为正常滚动条。这里动态加载xaml文件,设置ScrollViewer样式。
创建一个文件夹,命名为“Style”,拖到解决方案里,文件夹里添加ScrollViewer.xaml文件。
ScrollViewer.xaml,样式可以按照需要设置

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="ScrollBarThumb" TargetType="{x:Type Thumb}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Grid>
                        <Border Background="#7a7a7a" Opacity="0.75" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="HorizontalScrollBarPageButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Opacity" Value="0.2"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Rectangle Fill="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="VerticalScrollBarPageButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Opacity" Value="0.2"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Rectangle Fill="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="VerticalScrollBarPageButton2" TargetType="{x:Type RepeatButton}">
        <Setter Property="OverridesDefaultStyle"  Value="true"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Opacity"  Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Rectangle Fill="#90000000" Width="0" Height="0"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="for_scrollbar" TargetType="{x:Type ScrollBar}">
        <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="false"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Margin" Value="0,1,3,6"/>
        <Setter Property="Width"  Value="3"/>
        <Setter Property="MinWidth"  Value="3"/>
        <Setter Property="Opacity"   Value="0.2"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollBar}">
                    <Grid x:Name="Bg" SnapsToDevicePixels="true">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="auto"></RowDefinition>
                        </Grid.RowDefinitions>
                        <RepeatButton  Grid.Row="0" Style="{StaticResource VerticalScrollBarPageButton2}" Command="{x:Static ScrollBar.PageUpCommand}"/>
                        <Track x:Name="PART_Track"   Grid.Row="1" IsEnabled="{TemplateBinding IsMouseOver}" IsDirectionReversed="true">
                            <Track.DecreaseRepeatButton>
                                <RepeatButton Style="{StaticResource VerticalScrollBarPageButton}" Command="{x:Static ScrollBar.PageUpCommand}"/>
                            </Track.DecreaseRepeatButton>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton Style="{StaticResource VerticalScrollBarPageButton}" Command="{x:Static ScrollBar.PageDownCommand}"/>
                            </Track.IncreaseRepeatButton>
                            <Track.Thumb>
                                <Thumb Style="{StaticResource ScrollBarThumb}"/>
                            </Track.Thumb>
                        </Track>
                        <RepeatButton Grid.Row="2" Style="{StaticResource VerticalScrollBarPageButton2}" Command="{x:Static ScrollBar.PageDownCommand}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Orientation" Value="Horizontal">
                <Setter Property="Background"  Value="Transparent"/>
                <Setter Property="Margin" Value="1,0,6,1"/>
                <Setter Property="Height"   Value="5"/>
                <Setter Property="MinHeight"  Value="5"/>
                <Setter Property="Width"    Value="Auto"/>
                <Setter Property="Opacity" Value="0.2"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ScrollBar}">
                            <Grid x:Name="Bg" SnapsToDevicePixels="true">
                                <Track x:Name="PART_Track" IsEnabled="{TemplateBinding IsMouseOver}">
                                    <Track.DecreaseRepeatButton>
                                        <RepeatButton Style="{StaticResource HorizontalScrollBarPageButton}"  Command="{x:Static ScrollBar.PageLeftCommand}"/>
                                    </Track.DecreaseRepeatButton>
                                    <Track.IncreaseRepeatButton>
                                        <RepeatButton Style="{StaticResource HorizontalScrollBarPageButton}"  Command="{x:Static ScrollBar.PageRightCommand}"/>
                                    </Track.IncreaseRepeatButton>
                                    <Track.Thumb>
                                        <Thumb Style="{StaticResource ScrollBarThumb}"/>
                                    </Track.Thumb>
                                </Track>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="for_scrollviewer"
           TargetType="{x:Type ScrollViewer}">
        <Setter Property="BorderBrush" Value="LightGray"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                        <Grid Background="{TemplateBinding Background}">
                            <ScrollContentPresenter  Cursor="{TemplateBinding Cursor}" Margin="{TemplateBinding Padding}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                            <ScrollBar x:Name="PART_VerticalScrollBar"
                                       HorizontalAlignment="Right"
                                       Maximum="{TemplateBinding ScrollableHeight}"
                                       Orientation="Vertical"
                                       Style="{StaticResource for_scrollbar}"
                                       ViewportSize="{TemplateBinding ViewportHeight}"
                                       Value="{TemplateBinding VerticalOffset}"
                                       Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                            <ScrollBar x:Name="PART_HorizontalScrollBar"
                                       Maximum="{TemplateBinding ScrollableWidth}"
                                       Orientation="Horizontal"
                                       Style="{StaticResource for_scrollbar}"
                                       VerticalAlignment="Bottom"
                                       Value="{TemplateBinding HorizontalOffset}"
                                       ViewportSize="{TemplateBinding ViewportWidth}"
                                       Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="ScrollChanged" >
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_VerticalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.8"
                                        Duration="0:0:1"/>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_VerticalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.2"
                                        Duration="0:0:1"
                                        BeginTime="0:0:1"/>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_HorizontalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.8"
                                        Duration="0:0:1"/>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_HorizontalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.2"
                                        Duration="0:0:1"
                                        BeginTime="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseEnter"
                                      SourceName="PART_VerticalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_VerticalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.8"
                                        Duration="0:0:0.7"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseLeave"
                                      SourceName="PART_VerticalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_VerticalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.2"
                                        Duration="0:0:0.7"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseEnter"
                                      SourceName="PART_HorizontalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_HorizontalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.8"
                                        Duration="0:0:0.7"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseLeave"
                                      SourceName="PART_HorizontalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_HorizontalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.2"
                                        Duration="0:0:0.7"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

建立ScrollTemplate.cs文件

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

public enum ScrollViewer_Type
{
    Default = 0,
    Effect = 1,
    Win10 = 2,
}

public class ScrollTemplate : UserControl
{
    public ScrollViewer ScrollViewer = new ScrollViewer();
    private Style style;
    private double offset = 0;
    private bool show = false;

    public void InitializeScrollViewer(Canvas obj, FrameworkElement element, double left, double top, double width, double height, ScrollViewer_Type type)
    {
        var foo = new Uri("pack://application:,,,/Style/ScrollViewer.xaml", UriKind.RelativeOrAbsolute);
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo });
        style = (Style)FindResource("for_scrollviewer");

        ScrollViewer.Width = width;
        ScrollViewer.Height = height;
        
        Canvas.SetLeft(ScrollViewer, left);
        Canvas.SetTop(ScrollViewer, top);

        Canvas content = new Canvas()
        {
            Width = width,
            Height = element.Height+Canvas.GetTop(element),
        };
        ScrollViewer.Content = content;

        content.Children.Add(element);

        if (type == ScrollViewer_Type.Default) ScrollViewer.Style = null;
        if (type == ScrollViewer_Type.Effect) ScrollViewer.Style = style;
        if (type== ScrollViewer_Type.Win10)
        {
            ScrollViewer.Style = style;
            ScrollViewer.MouseMove += ScrollViewer_MouseMove;
        }

        obj.Children.Add(ScrollViewer);
    }

    private void ScrollViewer_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
        ScrollViewer pL = (ScrollViewer)sender;

        Point point = Mouse.GetPosition(e.Source as FrameworkElement);
        Rect rect = new Rect(pL.Width-17, 0, 17, pL.Height);

        if (rect.Contains(point))
        {
            if (!show)
            {
                offset = pL.VerticalOffset;
                pL.Style = null;
                pL.ScrollToVerticalOffset(offset);
                show = true;
            }
        }
        else
        {
            if (show)
            {
                offset = pL.VerticalOffset;
                pL.Style = style;
                pL.ScrollToVerticalOffset(offset);
                show = false;
            }
        }
    }
}

调用,比如要win10模式的。也可设置其他模式

ScrollTemplate scrollTemplate = new ScrollTemplate();
scrollTemplate.ScrollViewer.Background = Brushes.WhiteSmoke;
// 里面的组件
Border border = new Border()
{
    Width = 100,
    Height = 500,
    Background = Brushes.SteelBlue
};
Canvas.SetLeft(border,10.0);
Canvas.SetTop(border, 10.0);
// MainCanvan为窗体上的canvas
scrollTemplate.InitializeScrollViewer(MainCanvan, border,729,214,168,138, ScrollViewer_Type.Win10);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WPF(Windows Presentation Foundation)是一种用于创建图形用户界面的技术,它可以使用XAML(扩展应用程序标记语言)来定义用户界面。动态加载XAML控件是一种在运行时根据需要加载和显示控件的方法。 要动态加载XAML控件,首先需要创建一个XAML文件,其中包含要加载的控件的定义。接下来,可以使用XamlReader类的Load方法将XAML文件加载到应用程序中。 在加载XAML文件后,可以通过使用FindName方法来查找和访问已加载的控件。这样就可以在代码中对控件进行操作,例如更改属性值、添加事件处理程序等。 动态加载XAML控件的一个常见用途是在运行时根据条件或用户交互动态创建和显示控件。例如,可以根据用户选择的选项创建不同的控件,或者在特定条件下加载特定的控件。 当不需要或不再需要已加载的控件时,可以使用父级容器的Children属性或相关方法从容器中移除控件。 总的来说,通过使用WPFXAML技术,可以方便地实现动态加载XAML控件,以满足各种应用程序需求。通过在运行时加载和操作控件,可以实现更灵活和交互性的用户界面。 ### 回答2: WPF(Windows Presentation Foundation)是一种用于创建用户界面的技术,它使用XAML(可扩展应用程序标记语言)来定义界面布局和控件。动态加载XAML控件是指在程序运行时根据需要动态添加和显示控件。 在WPF中,可以通过使用XamlReader类的Load方法来动态加载XAML文件。首先,我们需要将XAML文件的内容存储在一个字符串中,然后使用XamlReader.Load方法加载并创建相应的控件对象。加载后的控件可以通过将其添加到父控件的子控件集合中来显示在界面上。 以下是一个简单的示例代码,演示了如何动态加载XAML控件: ```csharp string xaml = @"<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> 点击我 </Button>"; Button button = (Button)XamlReader.Load(new XmlTextReader(new StringReader(xaml))); // 将按钮添加到父控件的子控件集合中 parentControl.Children.Add(button); ``` 在上面的示例中,我们定义了一个简单的XAML字符串,描述了一个按钮控件。通过调用XamlReader.Load方法,并将XAML字符串作为参数传递给它,我们可以创建一个对应的Button控件对象。然后,将其添加到父控件的子控件集合中,即可将其显示在界面上。 通过动态加载XAML控件,我们可以根据需要在程序运行时根据用户的操作或其他条件动态创建和显示控件,从而实现更加灵活和交互性强的用户界面。这对于需要在运行时根据数据或逻辑条件进行界面布局和控制的应用程序非常有用。 ### 回答3: WPF(Windows Presentation Foundation)是一种用于构建Windows桌面应用程序的技术,并提供了丰富的布局和可视化功能。WPF允许动态加载XAML控件,这使得应用程序可以根据需要或某些条件来加载控件,从而提供更灵活和动态的用户界面。 动态加载XAML控件可以通过以下步骤完成: 1. 创建一个XAML文件,其中包含要动态加载的控件的定义。这个文件可以是独立的XAML文件,也可以是包含在应用程序的资源文件中。 2. 使用XamlReader类的Load方法,将XAML文件加载为一个对象。这个对象表示XAML中定义的控件树。 3. 将加载的控件树添加到应用程序的界面中。可以通过在代码中找到界面中的容器控件,然后将加载的控件树添加到这个容器中。 例如,假设我们有一个MainWindow.xaml文件,其中有一个Grid作为界面的容器。我们想要动态加载一个Button控件到这个Grid中。可以使用以下代码: ```csharp string xaml = "<Button Content='Dynamic Button' Width='100' Height='30' />"; object obj = XamlReader.Parse(xaml); Button button = (Button)obj; Grid grid = FindName("myGrid") as Grid; grid.Children.Add(button); ``` 在这个例子中,我们首先定义了一个XAML字符串,使用XamlReader的Parse方法将其转换为一个Button控件对象。然后,我们通过FindName方法找到名为"myGrid"的Grid,并将加载的Button控件添加到这个Grid中。 通过动态加载XAML控件,我们可以根据应用程序的需求来动态创建和更新用户界面,从而实现更加灵活和交互的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值