Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于文本框内容更改WPF控件(按钮)的已启用属性_C#_Wpf_Controls_Isenabled - Fatal编程技术网

C# 基于文本框内容更改WPF控件(按钮)的已启用属性

C# 基于文本框内容更改WPF控件(按钮)的已启用属性,c#,wpf,controls,isenabled,C#,Wpf,Controls,Isenabled,我在WPF中寻找一个解决方案,根据文本框的内容更改按钮的IsEnabled属性。文本框包含一个数值。如果该值大于某个值,则按钮的IsEnabled属性应设置为true,只要该属性低于该值,则该属性应为false。 我一直在四处寻找,但找不到合适的解决办法。我在这里找到的几乎就是我要找的。但问题是,这种方法只是检查文本框中是否有任何内容。但是我需要检查/比较数字内容 我更愿意找到一种在XAML中实现它的方法。或者,我也可以在ViewModel中实现它。但是我不知道怎么做!我正在考虑通过文本框中显示

我在WPF中寻找一个解决方案,根据文本框的内容更改按钮的IsEnabled属性。文本框包含一个数值。如果该值大于某个值,则按钮的IsEnabled属性应设置为true,只要该属性低于该值,则该属性应为false。 我一直在四处寻找,但找不到合适的解决办法。我在这里找到的几乎就是我要找的。但问题是,这种方法只是检查文本框中是否有任何内容。但是我需要检查/比较数字内容

我更愿意找到一种在XAML中实现它的方法。或者,我也可以在ViewModel中实现它。但是我不知道怎么做!我正在考虑通过文本框中显示的属性中的INotifyChanged事件通知按钮。但是我不知道怎么做

遵循一些代码。但是,对不起,文本框和按钮旁边什么都没有,因为我找不到解决这个问题的方法

<TextBox Name ="tbCounter" Text ="{Binding CalcViewModel.Counter, Mode=OneWay}" Background="LightGray" BorderBrush="Black" BorderThickness="1" 
              Height="25" Width="50"
              commonWPF:CTextBoxMaskBehavior.Mask="Integer"
              commonWPF:CTextBoxMaskBehavior.MinimumValue="0"
              commonWPF:CTextBoxMaskBehavior.MaximumValue="1000"
              IsReadOnly="True"/>
<Button Name="btnResetCount" Focusable="True" Content="Reset" Command="{Binding Path=CalcViewModel.ResetCounter}" Style="{StaticResource myBtnStyle}"
              Width="100" Height="25">

要组合元素属性绑定:

IsEnabled={Binding ElementName=Textbox, Path=Text}
使用值转换器

IsEnabled={Binding ElementName=Textbox, Path=Text, Converter={StaticResource IsAtLeastValueConverter}}
IsAtLeastValueConverter.cs

namespace WpfPlayground
{
    public class IsAtLeastValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (System.Convert.ToInt32(value) > 5)
            {
                return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
}
哦,我忘了您需要将此添加到控件中:

<Window.Resources>
    <wpfPlayground:IsAtLeastValueConverter x:Key="IsAtLeastValueConverter" />
</Window.Resources>


编辑:虚拟机版本

我已经在省略号(…)中添加了我没有对您的代码进行更改的内容

<Button ... IsEnabled={Binding Path=ButtonIsEnabled} ...>


class CalcViewModel:INotifyPropertyChanged
{
    private CCalc _calc;
    private bool _buttonIsEnabled;
    public ButtonIsEnabled {
        get { return _buttonIsEnabled; }
        set { 
            _buttonIsEnabled = value;
            RaisePropertyChanged("ButtonIsEnabled");
        }
    }

    public int Counter
    {
        get
        { return _calc.Counter; }
        set{ 
            _calc.Counter = value;
            _buttonIsEnabled = _calc.Counter > 5;
        }
    }
    ...
}

类CalcViewModel:INotifyPropertyChanged
{
私人CCalc_calc;
已启用私人bool\u按钮;
公共按钮已启用{
获取{return\u buttonIsEnabled;}
集合{
_按钮启用=值;
RaisePropertyChanged(“按钮启用”);
}
}
公共整数计数器
{
得到
{return\u calc.Counter;}
集合{
_计算计数器=数值;
_按钮启用=_计算计数器>5;
}
}
...
}
因此,这里发生的事情是,当您更改计数器值时,您设置ButtonIsEnabled属性,该属性引发property changed事件,并使用您用于确定是否应启用按钮的逻辑更新表单上的按钮


编辑:您可能需要从文本框中删除该Binding=单向,我不确定如果您使用该设置,它是否会启动set属性。

是否要组合元素属性绑定:

IsEnabled={Binding ElementName=Textbox, Path=Text}
使用值转换器

IsEnabled={Binding ElementName=Textbox, Path=Text, Converter={StaticResource IsAtLeastValueConverter}}
IsAtLeastValueConverter.cs

namespace WpfPlayground
{
    public class IsAtLeastValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (System.Convert.ToInt32(value) > 5)
            {
                return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
}
哦,我忘了您需要将此添加到控件中:

<Window.Resources>
    <wpfPlayground:IsAtLeastValueConverter x:Key="IsAtLeastValueConverter" />
</Window.Resources>


编辑:虚拟机版本

我已经在省略号(…)中添加了我没有对您的代码进行更改的内容

<Button ... IsEnabled={Binding Path=ButtonIsEnabled} ...>


class CalcViewModel:INotifyPropertyChanged
{
    private CCalc _calc;
    private bool _buttonIsEnabled;
    public ButtonIsEnabled {
        get { return _buttonIsEnabled; }
        set { 
            _buttonIsEnabled = value;
            RaisePropertyChanged("ButtonIsEnabled");
        }
    }

    public int Counter
    {
        get
        { return _calc.Counter; }
        set{ 
            _calc.Counter = value;
            _buttonIsEnabled = _calc.Counter > 5;
        }
    }
    ...
}

类CalcViewModel:INotifyPropertyChanged
{
私人CCalc_calc;
已启用私人bool\u按钮;
公共按钮已启用{
获取{return\u buttonIsEnabled;}
集合{
_按钮启用=值;
RaisePropertyChanged(“按钮启用”);
}
}
公共整数计数器
{
得到
{return\u calc.Counter;}
集合{
_计算计数器=数值;
_按钮启用=_计算计数器>5;
}
}
...
}
因此,这里发生的事情是,当您更改计数器值时,您设置ButtonIsEnabled属性,该属性引发property changed事件,并使用您用于确定是否应启用按钮的逻辑更新表单上的按钮


编辑:您可能需要从文本框中删除Binding=one,我不确定如果您使用该设置,它是否会启动set属性。

您应该将ViewModel更改为类似的内容

 public class  ViewModel:INotifyPropertyChanged  

        {
        public  int Counter
        {
            get { return _counter;  }
            set {
                _counter = value; 
                 RaisePropChanged("Counter");
                //for example 
                if (value>3)
                {
                    IsButtonCounterEnabled = true;  
                }
                else
                {
                    IsButtonCounterEnabled = false;  


                }
            }
        }
        public bool IsButtonCounterEnabled
        {
            get { return _IsButtonCounterEnabled;   }
            set { _IsButtonCounterEnabled = value;  
                  RaisePropChanged("IsButtonCounterEnabled");
                }
        }
        private  void RaisePropChanged(string propName)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(propName));   

        }


            public event PropertyChangedEventHandler PropertyChanged = delegate{};
            private int _counter;
            private bool _IsButtonCounterEnabled;
}
 <Button    IsEnabled="{Binding IsButtonCounterEnabled,Mode=OneWay}" Content="Button" HorizontalAlignment="Left" Height="47" Margin="215,57,0,0" VerticalAlignment="Top" Width="159"/>
然后像这样系上你的纽扣

 public class  ViewModel:INotifyPropertyChanged  

        {
        public  int Counter
        {
            get { return _counter;  }
            set {
                _counter = value; 
                 RaisePropChanged("Counter");
                //for example 
                if (value>3)
                {
                    IsButtonCounterEnabled = true;  
                }
                else
                {
                    IsButtonCounterEnabled = false;  


                }
            }
        }
        public bool IsButtonCounterEnabled
        {
            get { return _IsButtonCounterEnabled;   }
            set { _IsButtonCounterEnabled = value;  
                  RaisePropChanged("IsButtonCounterEnabled");
                }
        }
        private  void RaisePropChanged(string propName)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(propName));   

        }


            public event PropertyChangedEventHandler PropertyChanged = delegate{};
            private int _counter;
            private bool _IsButtonCounterEnabled;
}
 <Button    IsEnabled="{Binding IsButtonCounterEnabled,Mode=OneWay}" Content="Button" HorizontalAlignment="Left" Height="47" Margin="215,57,0,0" VerticalAlignment="Top" Width="159"/>


希望有此帮助

您应该将ViewModel更改为以下内容

 public class  ViewModel:INotifyPropertyChanged  

        {
        public  int Counter
        {
            get { return _counter;  }
            set {
                _counter = value; 
                 RaisePropChanged("Counter");
                //for example 
                if (value>3)
                {
                    IsButtonCounterEnabled = true;  
                }
                else
                {
                    IsButtonCounterEnabled = false;  


                }
            }
        }
        public bool IsButtonCounterEnabled
        {
            get { return _IsButtonCounterEnabled;   }
            set { _IsButtonCounterEnabled = value;  
                  RaisePropChanged("IsButtonCounterEnabled");
                }
        }
        private  void RaisePropChanged(string propName)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(propName));   

        }


            public event PropertyChangedEventHandler PropertyChanged = delegate{};
            private int _counter;
            private bool _IsButtonCounterEnabled;
}
 <Button    IsEnabled="{Binding IsButtonCounterEnabled,Mode=OneWay}" Content="Button" HorizontalAlignment="Left" Height="47" Margin="215,57,0,0" VerticalAlignment="Top" Width="159"/>
然后像这样系上你的纽扣

 public class  ViewModel:INotifyPropertyChanged  

        {
        public  int Counter
        {
            get { return _counter;  }
            set {
                _counter = value; 
                 RaisePropChanged("Counter");
                //for example 
                if (value>3)
                {
                    IsButtonCounterEnabled = true;  
                }
                else
                {
                    IsButtonCounterEnabled = false;  


                }
            }
        }
        public bool IsButtonCounterEnabled
        {
            get { return _IsButtonCounterEnabled;   }
            set { _IsButtonCounterEnabled = value;  
                  RaisePropChanged("IsButtonCounterEnabled");
                }
        }
        private  void RaisePropChanged(string propName)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(propName));   

        }


            public event PropertyChangedEventHandler PropertyChanged = delegate{};
            private int _counter;
            private bool _IsButtonCounterEnabled;
}
 <Button    IsEnabled="{Binding IsButtonCounterEnabled,Mode=OneWay}" Content="Button" HorizontalAlignment="Left" Height="47" Margin="215,57,0,0" VerticalAlignment="Top" Width="159"/>


如果您希望直接在XAML中进行验证(我不推荐这样做,因为验证可能应该在视图模型中进行),那么也可以使用包,例如-这允许您在绑定中编写一些C#(ish)

我以前使用过它,它可以使它变得非常简单,例如安装软件包,在应用程序的最开始添加行:

QuickConverter.EquationTokenizer.AddNamespace(typeof(object));
它将系统名称空间添加到QuickConverter(上面的行作为系统名称空间中的对象工作),然后您可以简单地执行以下操作:

IsEnabled="{qc:Binding 'Int32.TryParse($P) &amp;&amp; Int32.Parse($P) &gt;= 3', P={Binding ElementName=tbCounter, Path=Text}}"
如果
&破坏您的智能感知,您可以改为编写:

IsEnabled="{qc:Binding 'Int32.TryParse($P) ## Int32.Parse($P) &gt;= 3', P={Binding ElementName=tbCounter, Path=Text}}"
(其中
3
是您要测试的值)

编辑: 抱歉,在重读XAML时,可以更直接地编写如下内容:

IsEnabled="{qc:Binding '$P &gt;= 3', P={Binding CalcViewModel.Counter}}"

如果希望直接在XAML中进行验证(我不推荐这样做,因为验证可能应该在视图模型中进行),还可以使用一个包,例如-这允许您在绑定中编写一些C#(ish)

我以前使用过它,它可以使它变得非常简单,例如安装软件包,在应用程序的最开始添加行:

QuickConverter.EquationTokenizer.AddNamespace(typeof(object));
它将系统名称空间添加到QuickConverter(上面的行作为系统名称空间中的对象工作),然后您可以简单地执行以下操作:

IsEnabled="{qc:Binding 'Int32.TryParse($P) &amp;&amp; Int32.Parse($P) &gt;= 3', P={Binding ElementName=tbCounter, Path=Text}}"
如果
&破坏您的智能感知,您可以改为编写:

IsEnabled="{qc:Binding 'Int32.TryParse($P) ## Int32.Parse($P) &gt;= 3', P={Binding ElementName=tbCounter, Path=Text}}"
(其中
3
是您要测试的值)

编辑: 抱歉,在重读XAML时,可以更直接地编写如下内容:

IsEnabled="{qc:Binding '$P &gt;= 3', P={Binding CalcViewModel.Counter}}"

@鲍尔:谢谢。如果你能给我一个关于虚拟机实现的提示,那就太好了。我添加了相关的部分,我希望我没有遗漏任何内容或有打字错误。全班同学都可以在这里张贴。@ck84vi你看。我在底部添加了一个解释,解释了它的不同之处。@C鲍尔:非常感谢!如果您有任何其他问题,请随时发表评论。是的,我有:-)。它很好用。但问题是,我必须点击应用程序中的任何其他控件,才能在视觉上再次启动按钮。因为如果按钮被启用或禁用,我会更改样式。财产