基本信息
源码名称:Wpf 自定义滚动条样式模板
源码大小:5.96M
文件格式:.rar
开发语言:C#
更新时间:2018-04-21
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
参考网上资料,进行修改。定义了有上下按钮和无上下按钮2种滚动条样式模板,
每种滚动条定义7或8副图像,
半透明和无按钮的滚动条需Scrollbackground(x).png图像,上下按钮为全透明图像即可。
添加样式:
1、替换(x)为数字,例如(3),添加相应的图片到资源
2、调整滚动块高度和上下位置距离:
<Track.Thumb>
<Thumb Style="{StaticResource ThumbStyle(2)}"
Height="79"
Margin="-1,0,-1,0" />
</Track.Thumb>
设置上下按钮高度
<Grid x:Name="Bg"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<Grid.RowDefinitions>
<RowDefinition MaxHeight="39" />
<RowDefinition Height="0.00001*" />
<RowDefinition MaxHeight="39" />
</Grid.RowDefinitions>
如无上下按钮,则设置
<ImageBrush x:Key="ScrollBackground(x)"
TileMode="FlipY"
ImageSource="/Resource_ScrollImage/Scrollbackground(1).png" />
......
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="Bg"
Background="{DynamicResource ScrollBackground(1)}"
SnapsToDevicePixels="true">
......
背景透明
<ScrollBar x:Name="PART_VerticalScrollBar"
参考网上资料,进行修改。定义了有上下按钮和无上下按钮2种滚动条样式模板,
每种滚动条定义7或8副图像,
半透明和无按钮的滚动条需Scrollbackground(x).png图像,上下按钮为全透明图像即可。
添加样式:
1、替换(x)为数字,例如(3),添加相应的图片到资源
2、调整滚动块高度和上下位置距离:
<Track.Thumb>
<Thumb Style="{StaticResource ThumbStyle(2)}"
Height="79"
Margin="-1,0,-1,0" />
</Track.Thumb>
设置上下按钮高度
<Grid x:Name="Bg"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<Grid.RowDefinitions>
<RowDefinition MaxHeight="39" />
<RowDefinition Height="0.00001*" />
<RowDefinition MaxHeight="39" />
</Grid.RowDefinitions>
如无上下按钮,则设置
<ImageBrush x:Key="ScrollBackground(x)"
TileMode="FlipY"
ImageSource="/Resource_ScrollImage/Scrollbackground(1).png" />
......
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="Bg"
Background="{DynamicResource ScrollBackground(1)}"
SnapsToDevicePixels="true">
......
背景透明
<ScrollBar x:Name="PART_VerticalScrollBar"
Background="Transparent"
public partial class MainWindow : Window
{
bool change;
public string fontFace = Environment.CurrentDirectory @"\中文字体.ttf#萝莉体 第二版";
Grid scrollGrid;
ScrollViewer scrollViewer;
TextBlock textBlock;
public MainWindow()
{
InitializeComponent();
ImageBrush background = new ImageBrush();
background.ImageSource = new BitmapImage(new Uri(Environment.CurrentDirectory @"\background.jpg"));
background.Stretch = Stretch.Fill;
this.Background = background;
scrollGrid = (Grid)XamlToObj (@"
<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Name='scrollGrid'
HorizontalAlignment='Left'
VerticalAlignment='Top'
Margin='5'
Height='360'
Width='320'/>"
);
scrollViewer = (ScrollViewer)XamlToObj(@"
<ScrollViewer xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Name='scrollViewer'
HorizontalAlignment='Stretch'
VerticalAlignment='Stretch'
Margin='0'
Focusable='false'>
</ScrollViewer>
");
textBlock = (TextBlock)XamlToObj(@"
<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Name='textBlock'
HorizontalAlignment='Stretch'
VerticalAlignment='Stretch'
FontFamily='" fontFace @"'
FontSize='20'
Foreground='White'
Margin ='0'
TextWrapping = 'Wrap'
Text='" Res_Content.conter_1.Replace("\n", "

").Replace("\r", "

") @"'>
<TextBlock.Effect>
<DropShadowEffect Color='Black' BlurRadius='5' Opacity='0.8'></DropShadowEffect>
</TextBlock.Effect >
</TextBlock>
");
scrollViewer.Template = Resources["ScrollViewerControlTemplate(1)"] as ControlTemplate;
scrollViewer.Content = textBlock;
scrollGrid.Children.Add(scrollViewer);
GridMain.Children.Add(scrollGrid);
change = true;
}
public object XamlToObj(string xaml)
{
string strXaml = xaml;
StringReader strreader = new StringReader(strXaml);
XmlTextReader xmlreader = new XmlTextReader(strreader);
object obj = XamlReader.Load(xmlreader);
return obj;
}
private void btn_change_Click(object sender, RoutedEventArgs e)
{
change = !change;
if (change) scrollViewer.Template = Resources["ScrollViewerControlTemplate(1)"] as ControlTemplate;
else scrollViewer.Template = Resources["ScrollViewerControlTemplate(2)"] as ControlTemplate;
}
}