WPF中Slider控件的用法与详解

正在学习WPF,做个简单的记录

<Slider Maximun="100/>   最大行程 

TickPlacement 属性:可以增加刻度,可以控制在上方还是下方;

TickFrequency 属性:刻度值  默认为可以更改刻度数值;

IsSnapToTickEnabled 属性:默认值双精度小数,设置为true可以确保每次值都在对应的刻度值上;

2.联合textbox控件一起使用

<StackPanel>
    <TextBox x:Name="textBox1"  Text="{Binding Path=Value, 
ElementName=slider1,UpdateSourceTrigger=PropertyChanged}"
BorderBrush="Black"  Margin="5"/>
    <Slider x:Name="slider1"  Maximum="100"  Minimum="0"  Margin="5"
            TickFrequency="1" IsSnapToTickEnabled="True"/>
</StackPanel>

TickPlacement="BottomRight" 此属性可以不进行设置,就不在显示刻度, TickFrequency="5"属性改为1,即可实现整数

Textbox中UpdateSourceTrigger=PropertyChanged属性如果不设置,在Textbox中更改数值Slider的控件不会根据数值进行移动;

最后来看一下我们的实际使用效果吧!

<Window x:Class="WPF.Learn.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WPF.Learn"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="300">
    <Grid>
        <StackPanel Margin="10" VerticalAlignment="Center">
            <DockPanel VerticalAlignment="Center" Margin="10">
                <TextBlock Text="R" FontWeight="Bold" DockPanel.Dock="Left" VerticalAlignment="Center"/>
                <TextBox   Text="{Binding Path=Value, ElementName=slColorR,UpdateSourceTrigger=PropertyChanged}"
                          Margin="5" DockPanel.Dock="Right" TextAlignment="Right" Width="40"/>
                <Slider x:Name="slColorR"  Maximum="255"  Minimum="0"  Margin="5" 
                     TickFrequency="1" IsSnapToTickEnabled="True" ValueChanged="ColorSlider_ValueChanged"/>
            </DockPanel>
            
            <DockPanel VerticalAlignment="Center" Margin="10">
                <TextBlock Text="G" FontWeight="Bold" DockPanel.Dock="Left" VerticalAlignment="Center"/>
                <TextBox Text="{Binding Path=Value, ElementName=slColorG,UpdateSourceTrigger=PropertyChanged}"
              Margin="5" DockPanel.Dock="Right" TextAlignment="Right" Width="40"/>
                <Slider x:Name="slColorG"  Maximum="255"  Minimum="0"  Margin="5" 
         TickFrequency="1" IsSnapToTickEnabled="True" ValueChanged="ColorSlider_ValueChanged"/>
            </DockPanel>
            
            <DockPanel VerticalAlignment="Center" Margin="10">
                <TextBlock Text="B" FontWeight="Bold" DockPanel.Dock="Left" VerticalAlignment="Center"/>
                <TextBox Text="{Binding Path=Value, ElementName=slColorB,UpdateSourceTrigger=PropertyChanged}"
              Margin="5" DockPanel.Dock="Right" TextAlignment="Right" Width="40"/>
                <Slider x:Name="slColorB"  Maximum="255"  Minimum="0"  Margin="5" 
         TickFrequency="1" IsSnapToTickEnabled="True" ValueChanged="ColorSlider_ValueChanged"/>
            </DockPanel>
        </StackPanel>
    </Grid>
</Window>

Xaml界面的代码,下面是Cs的代码;

namespace WPF.Learn
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }
        private void ColorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Color color = Color.FromRgb((byte)slColorR.Value, (byte)slColorG.Value, (byte)slColorB.Value);
            this.Background = new SolidColorBrush(color);
        }
    }

效果图:

  • 27
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值