WPF/C#:数据绑定到方法

在WPF Samples中有一个关于数据绑定到方法的Demo,该Demo结构如下:

image-20240621101313200

运行效果如下所示:

来看看是如何实现的。

先来看下MainWindow.xaml中的内容:

<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>

在窗体资源中定义了一个ObjectDataProvider对象与DoubleToString对象。

ObjectDataProvider介绍

image-20240621102324090

ObjectDataProvider类用于包装和创建可以用作绑定源的对象。

 <ObjectDataProvider MethodName="ConvertTemp" >

image-20240621102953193

ObjectDataProvider.MethodName 属性获取或设置要调用的方法的名称。

<ObjectDataProvider.MethodParameters>
    <system:Double>0</system:Double>
    <local:TempType>Celsius</local:TempType>
</ObjectDataProvider.MethodParameters>

image-20240621103147579

ObjectDataProvider.MethodParameters属性获取要传递给方法的参数列表。

绑定到方法

<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>

绑定路径为方法的第一个参数。

使用的转换器是在资源中定义的DoubleToString

BindsDirectlyToSource属性设置为true,表示直接绑定到数据源对象,而不是是绑定到数据源对象的某个属性。

<Binding.ValidationRules>
     <local:InvalidCharacterRule/>
</Binding.ValidationRules>

image-20240621104346213

Binding.ValidationRules属性用于定义数据绑定的验证规则。这些规则用于在数据绑定过程中验证输入数据的有效性。如果数据不符合验证规则,WPF 可以显示错误信息或采取其他措施来处理无效数据。

这里使用的是InvalidCharacterRule对象。

<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>

ComboBox绑定到方法的第二个参数。

<Label Content="{Binding Source={StaticResource ConvertTemp}}"
Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"/>

绑定到方法的返回值。

现在查看这个方法:

  public string ConvertTemp(double degree, TempType temptype)
  {
      Type = temptype;
      switch (temptype)
      {
          case TempType.Celsius:
              return (degree*9/5 + 32).ToString(CultureInfo.InvariantCulture) + " " + "Fahrenheit";
          case TempType.Fahrenheit:
              return ((degree - 32)/9*5).ToString(CultureInfo.InvariantCulture) + " " + "Celsius";
      }
      return "Unknown Type";
  }

绑定过程梳理

该Demo启动后的界面如下所示:

image-20240621105239909

 <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>

ObjectDataProvider.MethodParameters中设置了方法的参数为0和Celsius。

而0是Double类型,TextBox需要string类型,因此设置了DoubleToString转换器。

该转换器代码如下所示:

 public class DoubleToString : IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter,
         CultureInfo culture) => value?.ToString();

     public object ConvertBack(object value, Type targetType, object parameter,
         CultureInfo culture)
     {
         var strValue = value as string;
         if (strValue != null)
         {
             double result;
             var converted = double.TryParse(strValue, out result);
             if (converted)
             {
                 return result;
             }
         }
         return null;
     }
 }
<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>

ComboBox的SelectedValue绑定到方法的第二个参数,刚开始也就是Celsius。

<Label Content="{Binding Source={StaticResource ConvertTemp}}"
Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"/>

绑定到方法的返回值。

在TextBox中输入了做了数据绑定的验证规则。

InvalidCharacterRule类代码如下所示:

internal class InvalidCharacterRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        var myvalue = 0.00;

        try
        {
            if (((string) value).Length > 0)
                myvalue = double.Parse((string) value);
        }
        catch (Exception e)
        {
            return new ValidationResult(false, "Illegal characters or " + e.Message);
        }

        return new ValidationResult(true, null);
    }
}

当输入不符合规则时,会有提示,如下图所示:

image-20240621110313599

代码来源

[WPF-Samples/Data Binding/BindingToMethod at main · microsoft/WPF-Samples (github.com)](https://github.com/microsoft/WPF-Samples/tree/main/Data Binding/BindingToMethod)

posted @ 2024-06-21 11:24  mingupupup  阅读(394)  评论(0编辑  收藏  举报