ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

WPF Slider滑块的使用

2021-06-14 09:32:43  阅读:291  来源: 互联网

标签:doubleSlider 滑块 ViewModel class Slider MainViewModel WPF data public


 

 

<Window x:Class="WpfApp1.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:WpfApp1"
        xmlns:local1="clr-namespace:WpfApp1.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="500">
    <Window.DataContext>
        <local1:MainViewModel></local1:MainViewModel>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Slider x:Name="sleder1" Value="{Binding IntSlider}" Minimum ="0" Maximum="100" SmallChange="1" LargeChange="1"  Grid.Row="0" VerticalAlignment="Center" Margin="40,0,40,0"></Slider>
        <TextBlock Text="{Binding ElementName=sleder1,Path=Value}" Grid.Row="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,10,0" ></TextBlock>

        <Slider x:Name="sleder2" Value="{Binding DoubleSlider}" Minimum ="0.01" Maximum="1" SmallChange="0.01" LargeChange="0.01" Grid.Row="1" VerticalAlignment="Center" Margin="40,0,40,0"></Slider>
        <TextBlock Text="{Binding ElementName=sleder2,Path=Value}" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,10,0" ></TextBlock>
    </Grid>
</Window>
前端代码
using GalaSoft.MvvmLight;
using System;

namespace WpfApp1.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}
        }

        private int intSlider;
        public int IntSlider
        {
            get { return intSlider; }
            set { intSlider = value; RaisePropertyChanged(() => IntSlider); }
        }

        private double doubleSlider;
        public double DoubleSlider
        {
            get { return Math.Round(doubleSlider, 2); }//保留两位小数点
            set { doubleSlider = value; RaisePropertyChanged(() => DoubleSlider); }
        }
    }
}
ViewModel.cs

 

标签:doubleSlider,滑块,ViewModel,class,Slider,MainViewModel,WPF,data,public
来源: https://www.cnblogs.com/lizhiqiang0204/p/14881850.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有