滑块控件和文本块控件交互-Metro 应用 [英] slider control and textblock control interaction-Metro apps

查看:25
本文介绍了滑块控件和文本块控件交互-Metro 应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 XAML 上的 {Binding} 将滑块控件设置为来自文本块的文本属性的值.

i am trying to set the slider control to value from text block's text property through {Binding} on XAML.

<Slider   ValueChanged="slider_ValueChanged_1"    Value= "{Binding ElementName=ComponentTextBlockValue,Path=Text}"   StepFrequency="25"/>  

我需要一个转换器来设置滑块的值吗?绑定有时似乎有效,但有时不起作用.有时,滑块只是没有将其值设置为文本块值.

Do i need an converter to set the slider's value . The binding seems to work sometimes but sometimes it doesn't work. Sometimes,the slider just doesn't set its value to text block value.

推荐答案

由于你直接绑定了 Slider 的值,没有使用值转换器,我怀疑当文本不是数字或超出范围时绑定被破坏了.

Since you bind the Slider's value directly without a value converter, I suspect that the binding is broken when the text is not a number or out of range.

>

您可以通过创建一个值转换器来防止这种情况发生,以防止绑定错误值,因此绑定将始终有效.

You can prevent that by creating a value converter that will prevent bad value to be bound, so the binding will always work.

这是一些例子:

public class TextToSliderValueConverter : IValueConverter
{
    public double MaximumSliderValue { get; set; }
    public double MinimumSliderValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        double sliderValue;

        if (double.TryParse(value as string, out sliderValue)
            && sliderValue <= MaximumSliderValue && sliderValue >= MinimumSliderValue)
        {
            return sliderValue;
        }
        else
        {
            return 0.0;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

这是 XAML:

<Page
    x:Class="stovfSliderTextBox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:stovfSliderTextBox"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <local:TextToSliderValueConverter x:Key="txtToSliderValue" MaximumSliderValue="100" MinimumSliderValue="0"/>
    </Page.Resources>
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel>
            <Slider Value= "{Binding ElementName=ComponentTextBlockValue,Path=Text, Converter={StaticResource txtToSliderValue}, ConverterParameter=slider}" StepFrequency="25"/>
            <TextBox x:Name="ComponentTextBlockValue" Width="50"/>
        </StackPanel>
    </Grid>
</Page>

TextToSliderValueConverter 确保滑块始终获得有效值.如果您不使用默认的 Slider.Maximum 或 Slider.Minimum,您可以相应地修改值.

The TextToSliderValueConverter makes sure that the slider will always get the valid value. If you do not use default Slider.Maximum or Slider.Minimum, you can modify the values accordingly.

希望这会有所帮助!

这篇关于滑块控件和文本块控件交互-Metro 应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆