Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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# 通过文本框的值启用/禁用按钮_C#_Wpf_Xaml - Fatal编程技术网

C# 通过文本框的值启用/禁用按钮

C# 通过文本框的值启用/禁用按钮,c#,wpf,xaml,C#,Wpf,Xaml,我在主窗口中有一个文本框 <TextBox Name="txtCalls" Text="{Binding ElementName=sliderCalls, Path=Value,UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" Width="22"/> 我想要的是通过文本框txtCalls的值来“启用”或“禁用”按钮。假设我们有这样的逻辑: if (txtCalls.Text == ""

我在主窗口中有一个文本框

<TextBox Name="txtCalls" Text="{Binding ElementName=sliderCalls,
            Path=Value,UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" Width="22"/>
我想要的是通过文本框
txtCalls
的值来“启用”或“禁用”按钮。假设我们有这样的逻辑:

if (txtCalls.Text == "")
{
    btnStart.IsEnabled = false;
    btnAbort.IsEnabled = true;
}
else
{
    btnStart.IsEnabled = true;
    btnAbort.IsEnabled = false;
}
顺便说一下,我有一门课:

public class NotifyUIBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
但是它被另一个combobox控件
公共类Combox:NotifyUIBase

我不知道如何在这种情况下应用文本框NotifyPropertyChanged。


   <TextBox Name="txtCalls"/>
    <Button Content="Start">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="IsEnabled" Value="True"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=txtCalls, Path=Text.Length}" Value="0">
                        <Setter Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
    <Button Content="Abort">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="IsEnabled" Value="False"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=txtCalls,Path=Text.Length}" Value="0">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
或者使用这个

<DataTrigger Binding="{Binding ElementName=txtCalls, Path=Text}" Value="{x:Static sys:String.Empty}">
                            <Setter Property="IsEnabled" Value="False"/>


如果其他人在寻找启用的按钮时遇到了这个问题,Maximus的答案会简单得多。这是一种更注重MVVM的方式来处理这种情况

所以我知道这里的名字是值得期待的,但它们确实做了一些简单的事情。两个按钮IsEnabled属性都绑定到处理文本框的文本值。然后转换器接收该值,对照目标字符串检查该值,然后返回一个值,指示是否应启用该按钮。为此,它们可以很容易地命名为“IfValueisProcessingtheTrueConverter”或类似的名称,但这看起来也很混乱

MainWindow.xaml

<Window x:Class="WpfPlayground.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfPlayground="clr-namespace:WpfPlayground"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <wpfPlayground:CanStartConverter x:Key="CanStartConverter" />
        <wpfPlayground:CanAbortConverter x:Key="CanAbortConverter" />
    </Window.Resources>
    <StackPanel>
        <Button x:Name="btnStart" Margin="5,12,5,0.2" Content="Start Test" Width="78" DockPanel.Dock="Left" 
        IsEnabled="{Binding ElementName=txtCalls, Path=Text, Converter={StaticResource CanStartConverter}}"
        Click="btnStart_Click"  VerticalContentAlignment="Center"/>
        <Button  x:Name="btnAbort" Content="Abort Test" Width="76" DockPanel.Dock="Right"
         IsEnabled="{Binding ElementName=txtCalls, Path=Text, Converter={StaticResource CanAbortConverter}}" Click="btnAbort_Click" VerticalContentAlignment="Center" Margin="0,12,0,0.2"/>
        <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="{Binding ElementName=sliderCalls,
        Path=Value,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120" Name="ProcessingTextBox"/>

    </StackPanel>
</Window>
CanAbortConverter.cs

using System;
using System.Globalization;
using System.Windows.Data;

namespace WpfPlayground
{
    public class CanStartConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.ToString() != "")
            {
                return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
}
using System;
using System.Globalization;
using System.Windows.Data;

namespace WpfPlayground
{
    public class CanAbortConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.ToString() == "")
            {
                return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }

    }
}

我已经定义了两个按钮,我不想更改它们,因为它们也将在其他地方设置。查看我的更新代码。请根据我的描述更新代码。当文本框值为时!=0时,启动按钮启用,中止按钮禁用。谢谢你更新代码,我会试试的。好的,我做了一些编辑,以更符合你的场景。但我不能100%确定我是否得到了它。您可以在一行代码中得到它,返回String.IsNullOrEmpty(value.ToString())?真:假;还没有,就像我说的。我不想改变我原来的按钮的描述。如果你这么喜欢它的话。没有变化就没有收获。
using System;
using System.Globalization;
using System.Windows.Data;

namespace WpfPlayground
{
    public class CanAbortConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.ToString() == "")
            {
                return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }

    }
}