silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了wpf – 将Button的可见性绑定到两个文本框的内容的最简洁方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我的应用程序中有一个Button,其“启用”我已经绑定TextBox是否为空,如下所示: <Button Content="Go" IsEnabled="{Binding Text, Converter={Staticresource EnableIfStringNotEmptyDatasourcE},
我的应用程序中有一个Button,其“启用”我已经绑定Text@L_675_1@是否为空,如下所示:

<Button Content="Go" 
        IsEnabled="{Binding Text,Converter={Staticresource EnableIfStringNotEmptyDatasourcE},ElementName=myText@L_675_1@}"/>

(为简洁起见,省略了EnableIfStringNotEmptyDatasource的实现).

当Text@L_675_1@的文本发生更改时,这会自动更改状态.

以同样的方式将属性绑定到两个文本框是否为空的最优雅的方法是什么?

解决方法

编辑:对于silverlight来说,这可能是最干净的:

<Text@L_675_1@ Text="{Binding TB1Text,Mode=TwoWay}" />
<Text@L_675_1@ Text="{Binding TB2Text,Mode=TwoWay}"/>
<Button Content="Lorem Ipsum" IsEnabled="{Binding ButtonIsEnableD}"/>
private String _TB1Text;
public String TB1Text
{
    get { return _TB1Text; }
    set
    {
        if (_TB1Text != value)
        {
            _TB1Text = value;
            PropertyChanged.Notify(() => this.TB1Text);
            PropertyChanged.Notify(() => this.buttonIsEnabled);
        }
    }
}

private String _TB2Text;
public String TB2Text
{
    get { return _TB2Text; }
    set
    {
        if (_TB2Text != value)
        {
            _TB2Text = value;
            PropertyChanged.Notify(() => this.TB2Text);
            PropertyChanged.Notify(() => this.buttonIsEnabled);
        }
    }
}

public bool ButtonIsEnabled
{
    get { return !(String.IsNullOrEmpty(TB1Text) && String.IsNullOrEmpty(TB2Text)); }
}

(PropertyChanged.Notify只是一种扩展方法,可以在不需要传递字符串的情况下引发事件)

不会使用绑定而是使用MultiDatatrigger:

<Text@L_675_1@ Name="tb1"/>
<Text@L_675_1@ Name="tb2"/>
<Button Content="Lorem Ipsum">
    <Button.Style>
        <Style TargetType="Button">
            <Style.triggers>
                <MultiDatatrigger>
                    <MultiDatatrigger.Conditions>
                        <Condition Binding="{Binding Text,ElementName=tb1}" Value="{x:Static sys:string.Empty}"/> 
                        <Condition Binding="{Binding Text,ElementName=tb2}" Value="{x:Static sys:string.Empty}"/> 
                    </MultiDatatrigger.Conditions>
                    <Setter Property="IsEnabled" Value="false"/>
                </MultiDatatrigger>
            </Style.triggers>
        </Style>
    </Button.Style>
</Button>

便说一句,你不需要一个Text@L_675_1@案例的转换器:

<Button Content="Lorem Ipsum" IsEnabled="{Binding Text.Length,ElementName=myText@L_675_1@}"/>

大佬总结

以上是大佬教程为你收集整理的wpf – 将Button的可见性绑定到两个文本框的内容的最简洁方法全部内容,希望文章能够帮你解决wpf – 将Button的可见性绑定到两个文本框的内容的最简洁方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。