WPF:数据绑定示例总结(1)

BindConversion绑定转换

clipboard.png

ps:type.Name

public object Convert(object o, Type type,
            object parameter, CultureInfo culture)
{
    var date = (DateTime) o;
    switch (type.Name)
    {
        case "String":
            return date.ToString("F", culture);
        case "Brush":
            return Brushes.Red;
        default:
            return o;
    }
}

public object ConvertBack(object o, Type type,
    object parameter, CultureInfo culture) => null;
}
<TextBlock Text="Unconverted data:"/>
    <TextBlock Text="{Binding Path=TheDate}"/>
    <TextBlock Text="Converted data:"/>
    <TextBlock Name="myconvertedtext"
        Foreground="{Binding Path=TheDate,
        Converter={StaticResource MyConverterReference}}">
        <TextBlock.Text>
            <Binding Path="TheDate"
           Converter="{StaticResource MyConverterReference}"/>
        </TextBlock.Text>
    </TextBlock>

BindingDPToDP属性间绑定

clipboard.png

<Canvas.Background>
    <Binding ElementName="myComboBox" Path="SelectedItem.Content"/>
</Canvas.Background>

也可以选择为:SelectedValue.Content
或者:SelectionBoxItem:获取在选择框中显示的项。(默认字符串值)

BindingToMethod绑定方法

Binding.BindsDirectlyToSource 属性:
获取或设置一个值,该值指示是否计算相对于数据项或 DataSourceProvider 对象的 Path。

clipboard.png

clipboard.png

  1. 在本例中,TemperatureScale 是一个类,它有一个方法 ConvertTemp,该方法接收两个参数(分别为 double 类型和 enum 类型的 TempType),并将给定值从一个温标转换为另一个温标。
  2. ObjectDataProvider 用于实例化 TemperatureScale 对象。 将使用两个指定参数调用 ConvertTemp 方法。
<Window.Resources>
  <ObjectDataProvider ObjectType="{x:Type local:TemperatureScale}"
                      MethodName="ConvertTemp" x:Key="convertTemp">
    <ObjectDataProvider.MethodParameters>
      <system:Double>0</system:Double>
      <local:TempType>Celsius</local:TempType>
    </ObjectDataProvider.MethodParameters>
  </ObjectDataProvider>

  <local:DoubleToString x:Key="doubleToString" />

</Window.Resources>

方法可以作为资源使用,因此可绑定到其结果。

  1. 示例中, TextBox 的 Text 属性和 ComboBox 的 SelectedValue 绑定到方法的两个参数。
  2. 转换器 DoubleToString 接收一个 double 类型的数据,并以 Convert 方向(从绑定资源到绑定目标,绑定目标是 Text 属性)将其转换为 string 类型,并以 ConvertBack 方向将 string 转换为 double。
  3. InvalidationCharacterRule 是一个 ValidationRule,用于检查无效字符。 默认的错误模板是一个围绕在 TextBox 四周的红色边框,用于在输入值不是一个 double 类型的值时向用户发出通知。
<Label Grid.Row="1" HorizontalAlignment="Right">Enter the degree to convert:</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="tb">
  <TextBox.Text>
    <Binding Source="{StaticResource convertTemp}" Path="MethodParameters[0]"
             BindsDirectlyToSource="true" UpdateSourceTrigger="PropertyChanged"
             Converter="{StaticResource doubleToString}">
      <Binding.ValidationRules>
        <local:InvalidCharacterRule/>
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>
<ComboBox Grid.Row="1" Grid.Column="2" 
  SelectedValue="{Binding Source={StaticResource convertTemp},
  Path=MethodParameters[1], BindsDirectlyToSource=true}">
  <local:TempType>Celsius</local:TempType>
  <local:TempType>Fahrenheit</local:TempType>
</ComboBox>
<Label Grid.Row="2" HorizontalAlignment="Right">Result:</Label>
<Label Content="{Binding Source={StaticResource convertTemp}}"
    Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值