当所有文本框在WPF中具有值时,需要启用该按钮 [英] Need to enable the button when all textbox has value in WPF

查看:65
本文介绍了当所有文本框在WPF中具有值时,需要启用该按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码的问题是当我在任何一个文本框中输入值时,按钮被启用,我想要的是只有当所有文本框都有一些值时才应启用该按钮。如果我遗漏任何东西,请帮助我。

The problem with the below code is when I am entering the value in any one of the textboxes , the button is getting enabled and what I want is that the button should be enabled only when all the textbox has some value. Please help me if I am missing anything.

<Window.Resources><Style TargetType="TextBox">
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="Text" Value=""/>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters>
                        <Setter Property="Button.IsEnabled" Value="False"/>
                    </MultiTrigger.Setters>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid Margin="0,192,0,0">
        <TextBox x:Name="txt_Titel" Margin="137,-149,260,241"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="137,-91,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="137,-63,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
        <Button Content="Transfer" />
    </Grid>
</Window>

推荐答案

你可以为此目的使用 MultiBinding 。您可以按如下方式创建 IMultiValueConverter

You can use a MultiBinding for that purpose. You can create an IMultiValueConverter as follows:

public class HasAllTextConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool res = true;

        foreach (object val in values)
        {
            if (string.IsNullOrEmpty(val as string))
            {
                res = false;
            }
        }

        return res;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

并使用 MultiBinding ,如下所示:

<StackPanel>
    <StackPanel.Resources>
        <local:HasAllTextConverter x:Key="HasAllTextConverter" />
    </StackPanel.Resources>
    <TextBox Name="tb1" />
    <TextBox Name="tb2" />
    <Button Content="Button 1" >
        <Button.IsEnabled>
            <MultiBinding Converter="{StaticResource HasAllTextConverter}">
                <Binding ElementName="tb1" Path="Text" />
                <Binding ElementName="tb2" Path="Text" />
            </MultiBinding>
        </Button.IsEnabled>
    </Button>
</StackPanel>

如果您不想使用代码隐藏,可以使用 MultiTrigger (但是,由于面板的触发器只能包含 EventTrigger ,你必须用 DataTemplate 包装它或其他可以包含它的东西,也包含所涉及的元素)。类似于:

If you don't want to use code-behind, you can do it with a MultiTrigger (but, since panel's triggers can contain only an EventTrigger, you have to wrap it with a DataTemplate or something else that can contain it and, contains the involved elements too). Something like:

<ContentControl Content="{Binding}">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBox Name="tb3" />
                <TextBox Name="tb4" />
                <Button Name="btn2" Content="Button 2" />
            </StackPanel>
            <DataTemplate.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition SourceName="tb3" Property="TextBox.Text" Value="" />
                        <Condition SourceName="tb4" Property="TextBox.Text" Value="" />
                    </MultiTrigger.Conditions>
                    <Setter TargetName="btn2" Property="IsEnabled" Value="False" />
                </MultiTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>


我能看到的第一个缺失的部分是你在字典中创建了样式,但没有在任何地方使用它。



首先,你需要给你的风格的。我们假设您要将其命名为myTextBoxStyle,然后:

The first missing part I can see is that you created the style in the dictionary but did not use it anywhere.

First, you need to give a key to your style. Let's assume you want to name it "myTextBoxStyle", then:
<Style x:Key="myTextBoxStyle" TargetType="TextBox">
   <!-- ... -->
</Style>



然后,对于 TextBox ,使用你要添加属性的样式 Style ={StaticResource myTextBoxStyle}







实际上,没有你的项目甚至无法建立。



-SA


Then, for the TextBox using the style you have a to add the attribute Style="{StaticResource myTextBoxStyle}".



Actually, without key your project could not even build.

—SA


您好谢谢谢谢我试过这个让我知道我在哪里做错了。

Hi Sergey i ahve tried this let me know where i am doing wrong.
<Grid Margin="0,192,0,0">
        <TextBox x:Name="txt_Titel" Margin="137,-149,260,241"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="137,-91,0,0" Text="" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="137,-63,0,0" Text="" VerticalAlignment="Top" Width="120"/>
        <Button Content="Transfer">
            <Button.Style>
                <Style>
                    <Style.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="TextBox.Text" Value=""/>
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter Property="Button.IsEnabled" Value="False"/>
                            </MultiTrigger.Setters>
                        </MultiTrigger>
                        
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
    </Grid>


这篇关于当所有文本框在WPF中具有值时,需要启用该按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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