如何使用启用/禁用按钮WPF强制使用时间跨度 [英] How to force timespan to be depended with enabled/disabled button WPF

查看:162
本文介绍了如何使用启用/禁用按钮WPF强制使用时间跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a TimeSpan textbox, my objectif is to disable the button Save when the form of TIMESPAN is wrong (exp 90:00:00)..

I try a code, it is correct for just once time..if I set 20:10:00 ..The Save button is enabled (correct). After that however the TIMESPAN is wrong 55:00:00, the button is enabled ( and the saving in database is 00:00:00)










How I can fix it to be work always correct? Thanks,





我尝试过:



XAML:



What I have tried:

The XAML:

<TextBox Name="txtTime"  Margin="10,10,10,10" >
                <TextBox.Text >
                    <Binding Path="Time" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True" Mode="TwoWay" >
                        <Binding.ValidationRules>
                            <local:DateTimeValidationRule ValidationStep="RawProposedValue"/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>







The ViewModel :







public bool  VarTIME ;



  [Required(ErrorMessage = "Time is required")]
    public TimeSpan Time
    {
        get { return time; }
        set
        {
            time = value;
            intervalString = Time.ToString();
            TimeSpan reded;
            bool success = TimeSpan.TryParseExact(intervalString, "hh\\:mm\\:ss",
                           CultureInfo.InvariantCulture, out reded);

            if (success)
            {
                VarTIME = true;
            }
            OnPropertyChanged("Time");
        }
    }


    public SheduleTrainViewModel()
    {
        VarTIME = false;
        addTrain = new RelayCommand<string>(AddTrainFunction, canAddTrain);

        private bool canAddTrain(string obj)
         {     return VarTIME;     

         }
    }

推荐答案

在这种情况下看起来它失败了,因为你是失败时不将VarTIME设置为false。这应该解决它:



In this case it looks like it's failing because you're not setting VarTIME back to false on a failure. This should fix it:

public TimeSpan Time
{
    get { return time; }
    set
    {
        time = value;
        intervalString = Time.ToString();
        TimeSpan reded;

        VarTIME = TimeSpan.TryParseExact(intervalString, "hh\\:mm\\:ss",
                  CultureInfo.InvariantCulture, out reded);

        OnPropertyChanged("Time");
    }
}





话虽如此,我认为有更好的方法。 ..这里的setter方法回调属性,它解析的数据没有被设置回支持者,它设置其他变量......



另外我想你可能已经错过了TimeSpan是一个结构,所以它总是有一个值,因此需要在这里不起作用。此外,出于输入目的,默认值0.00:00:00可能有效。



如果您想确保时间少于一天,请使用必需的最大属性。将此转换为数据库模型时,如果不需要,可以截断小数秒。



为了确保它有效,我会采用一种简单的方法使用是TimeSpan?转换器,如果格式不符合预期,则返回null。然后更改命令以获取TimeSpan?如果为null则返回false。



对于更优雅的解决方案,我会在这里查看一些验证示例:



WPF TextBox验证C# - 堆栈溢出 [ ^ ]


这篇关于如何使用启用/禁用按钮WPF强制使用时间跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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