按文本框的值启用/禁用按钮

时间:2014-08-28 13:55:54

标签: c# wpf xaml

我在MainWindow中有一个文本框。

<TextBox Name="txtCalls" Text="{Binding ElementName=sliderCalls,
            Path=Value,UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" Width="22"/>

此外,我还有两个按钮:btnStartbtnAbort

<Button x:Name="btnStart" Margin="5,12,5,0.2" Content="Start Test" Width="78" DockPanel.Dock="Left"
            Click="btnStart_Click"  VerticalContentAlignment="Center"/>

<Button  x:Name="btnAbort" Content="Abort Test" Width="76" DockPanel.Dock="Right"
            IsEnabled="False" Click="btnAbort_Click" VerticalContentAlignment="Center" Margin="0,12,0,0.2"/>

我想要的是通过文本框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));
        }
    }
}

但它由另一个组合框控件public class Combox : NotifyUIBase

使用

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

2 个答案:

答案 0 :(得分:1)

   <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"/>

答案 1 :(得分:1)

如果其他人在寻求启用按钮时遇到了这个答案,那么Maximus的答案会更容易。这是一种更专注于MVVM的处理方式。

所以我知道这里的名字是值得期待的,但他们真的做的很简单。两个按钮IsEnabled属性都绑定到处理文本框的Text值。然后转换器接收值,根据目标字符串检查它,然后返回一个值,指示是否应该启用该按钮。为此,他们很容易被命名为“IfValueIsProcessingThenTrueConverter”或类似的东西,但这似乎也很混乱。

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>

CanStartConverter.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;
        }
    }
}

CanAbortConverter.cs

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;
        }

    }
}