WPF 多绑定和转化器


将两个 textBox 的Text属性绑定到 Button的IsEnabled

如果text为空则Button不可用


<Window x:Class="MultiBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:loc="clr-namespace:MultiBinding"
        Title="MainWindow" Height="350" Width="525"      
        >
    <Window.Resources>
        <loc:StringBooleanConverter x:Key="StringBooleanConverter"/>
    </Window.Resources>
    <Grid>
        <TextBox Name="txt1" HorizontalAlignment="Left" Height="23" Margin="126,80,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
        <TextBox Name="txt2" HorizontalAlignment="Left" Height="23" Margin="126,117,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="126,174,0,0" VerticalAlignment="Top" Width="75">
            <Button.IsEnabled>
                <MultiBinding Converter="{StaticResource StringBooleanConverter}">
                    <Binding ElementName="txt1" Path="Text"/>
                    <Binding ElementName="txt2" Path="Text"/>
                </MultiBinding>
            </Button.IsEnabled>
        </Button>
    </Grid>
</Window>



namespace MultiBinding
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class StringBooleanConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool flag = true;
            foreach (object value in values)
            {
                string str = value as string;
                if (str == string.Empty)
                {
                    flag= false;
                    break;
                }
            }
            return flag;
        }


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


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值