WPF中的多绑定实现

效果如下:

左边3个文本框信息,下面的文本框绑定上2个文本框信息

<StackPanel HorizontalAlignment="Left"

                    Height="230"

                    Margin="10 10 0 0"

                    VerticalAlignment="Top" Width="366">

  <TextBox x:Name="txtFname" Text="FName"/>

  <TextBox x:Name="txtLname" Text="LName"/>

  <TextBox>

    <TextBox.Text>

      <MultiBinding Converter="{StaticResource FLName2FullName}">

          <Binding ElementName="txtFname" Path="Text"/>

          <Binding ElementName="txtLname" Path="Text"/>

      </MultiBinding>

    </TextBox.Text>

  </TextBox>

</StackPanel>

实现多绑定转换类:

using System.Windows.Data;

public class FirstNameLastNameToFullNameConverter : IMultiValueConverter
{
  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  {
    if (values != null)
    {
        return values[0].ToString() + " " + values[1].ToString();
    }
    else
    {
        return null;
    }
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
   string[] values = null;
    if (value != null && value.ToString() != "")
    {
      return values = value.ToString().Split(' ');
    }
    else
    {
        return null;

    }
  }
}

右边部分是CheckBox的多绑定

<StackPanel HorizontalAlignment="Left"

                    Height="232"

                    Margin="408 8 0 0"

                    VerticalAlignment="Top" Width="382">

<TextBlock TextWrapping="Wrap" Text="Which statements are True? select all that apply"/>

  <CheckBox x:Name="chk1" Content="target binds to multiple source"/>

  <CheckBox x:Name="chk2" Content="binding can not done inline"/>

  <CheckBox x:Name="chk3" Content="requires multbinding"/>

      <TextBlock>

        <TextBlock.Text>

          <MultiBinding Converter="{StaticResource bools2String}">

                <Binding ElementName="chk1" Path="IsChecked"/>

                <Binding ElementName="chk2" Path="IsChecked"/>

                <Binding ElementName="chk3" Path="IsChecked"/>

          </MultiBinding>

          </TextBlock.Text>

      </TextBlock>

</StackPanel>

转换类:

using System.Windows.Data;

public class BoolsToStringConverter : IMultiValueConverter
{
  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
  string answer = " You answer is : ";
  if ((bool)values[0] == true && (bool)values[1] == true && (bool)values[2] == true)
  {
    return answer + "CORRECT";
  }
  else if ((bool)values\[0\] == false && (bool)values\[1\] == false && (bool)values\[2\] == false)
  {
      return answer;
  }
  else
  {
    return answer + "INCORRECT";
  }
}

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

资源设置:

<Window.Resources>

  <Style TargetType="StackPanel">

      <Setter Property="Background" Value="Beige"/>

  </Style>

  <Style TargetType="CheckBox">

      <Setter Property="Margin" Value="5"/>

      <Setter Property="FontSize" Value="20"/>

      <Setter Property="FontWeight" Value="Bold"/>

  </Style>

  <Style TargetType="TextBlock">

      <Setter Property="Margin" Value="3"/>

      <Setter Property="FontSize" Value="20"/>

  </Style>

    <Style TargetType="TextBox">

      <Setter Property="Margin" Value="3"/>

      <Setter Property="FontSize" Value=" 16"/>

    </Style>

<local:FirstNameLastNameToFullNameConverter x:Key="FLName2FullName"></local:FirstNameLastNameToFullNameConverter>

<local:BoolsToStringConverter x:Key="bools2String"/>

</Window.Resources>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值