WPF中的多绑定实现

效果如下:

WPF中的多绑定实现_第1张图片

左边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">

      "Background" Value="Beige"/>

  Style>

  <Style TargetType="CheckBox">

      "Margin" Value="5"/>

      "FontSize" Value="20"/>

      "FontWeight" Value="Bold"/>

  Style>

  <Style TargetType="TextBlock">

      "Margin" Value="3"/>

      "FontSize" Value="20"/>

  Style>

    <Style TargetType="TextBox">

      "Margin" Value="3"/>

      "FontSize" Value=" 16"/>

    Style>

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

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

Window.Resources>

你可能感兴趣的:(C#,编程,wpf,多绑定)