所以,我有一个带有转换器的多绑定,它接受一些值并找到它们的最大值。问题是其中一个绑定使用的转换器需要double
目标类型,而绑定具有object
目标类型。我想知道是否有任何方法可以以任何方式修改绑定的目标类型。
下面是我的xaml的近似值:
<TextBlock>
<TextBlock.Width>
<MultiBinding Converter="{StaticResource _maxValueConverter}">
<Binding Source="{StaticResource _constantZeroValue}"/>
<Binding Path="ActualWidth"
ElementName="_previousTextBlock"
Converter="{StaticResource _requiresDoubleTargetConverter}"/>
</MultiBinding>
</TextBlock.Width>
</TextBlock>
所以基本上如果有任何方法可以告诉第二个绑定它输出的是双值,那就太好了。
最小可验证完整示例:
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication1"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.Resources>
<sys:Double x:Key="constantZero">0</sys:Double>
<local:RequiresDoubleTargetConverter x:Key="requiresDoubleTargetConverter" />
<local:MaxValueConverter x:Key="maxValueConverter" />
</StackPanel.Resources>
<Border x:Name="topBorder"
BorderThickness="1"
BorderBrush="Black"
HorizontalAlignment="Left">
<TextBlock x:Name="topTextBlock"
Background="Aqua"
Text="{Binding TopText}" />
</Border>
<Border BorderThickness="1"
BorderBrush="Black"
MinWidth="100"
HorizontalAlignment="Left">
<TextBlock Background="ForestGreen"
Text="{Binding BottomText}"
TextWrapping="Wrap"
MinWidth="100">
<TextBlock.Width>
<MultiBinding Converter="{StaticResource maxValueConverter}">
<MultiBinding.Bindings>
<Binding Path="ActualWidth" ElementName="topTextBlock" Converter="{StaticResource requiresDoubleTargetConverter}" />
<Binding Source="{StaticResource constantZero}" />
</MultiBinding.Bindings>
</MultiBinding>
</TextBlock.Width>
</TextBlock>
</Border>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public string TopText
{
get { return "Hello World!"; }
}
public string BottomText
{
get { return "hi earth."; }
}
public MainWindow()
{
InitializeComponent();
}
}
public class RequiresDoubleTargetConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// I am looking for a way to manually ensure that "targetType == typeof(double)" evaluates to true.
if (targetType != typeof(double))
{
return null;
}
else
{
// Actual converter performs this calculation.
return (double)value - 14;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// Irrelevant method for our purposes.
throw new NotImplementedException();
}
}
public class MaxValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
double max = double.NegativeInfinity;
foreach (object value in values)
{
if (value is double)
{
max = Math.Max((double)value, max);
}
else
{
Debug.Fail("All values must be doubles");
}
}
return max;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
// Irrelevant method for our purposes.
throw new NotImplementedException();
}
}
}
这是使用Visual Studio 2015创建的,并经过验证可以显示错误行为。我想要确定的是,是否可以从xaml手动设置targetType
的RequiresDoubleTargetConverter
。
就类型系统而言,绑定在object
上运行。如果你想要一个特定的类型,你需要确保自己。
但是,您可以使用传递给转换器的目标类型来确定所需的类型,并相应地修改转换器的返回值。
那么有没有办法手动设置转换器的目标类型,还是我坚持认为它是对象?
由于object
方法的签名始终是相同的,即它接受Convert
的值而没有其他任何东西,因此你坚持使用object[]
。
您必须在values[1]
方法中将double
强制转换为Convert
(因为该方法将始终只传递object
类型的值):
double d = (double)values[1];
我已经想出了解决这个问题的方法,但是只有你可以访问你在MultiBinding上使用的转换器(或者你可以添加一个转换器),它才有效。如果它还使用了一个额外的工作量。 ConverterParameter,TargetNullValue和/或StringFormat。
诀窍是当您将子Binding添加到MultiBinding时,您从该子绑定中删除Converter,ConverterParameter,TargetNullValue和StringFormat值,并将它们存储在MultiBinding转换器的Convert方法可访问的位置。 (我们使用包装器MarkupExtension来模拟MultiBinding,这样我们就可以在实际应用之前访问所有内容,但不能更改它们。)
然后在MultiBinding的Convert方法中,您现在从子绑定中获取原始的,尚未转换/格式化/合并的值,但是您还拥有所需的最终目标(在此示例中为double)到您所在的MultiBinding的Convert方法。
使用该信息,然后手动调用子转换器的Convert方法,传入尚未转换的值,targetType(传入给您)和childConverterParameter。
您获取该调用的结果,如果为null,则从子绑定返回TargetNullValue。
如果它不是null并且targetType都是字符串并且您具有字符串格式,则最后格式化结果。
这是伪代码(即我的头顶。修改了很多语法错误等等。对于实际的代码,你可以看到我在我的DynamicResourceBinding
类中使用它,我在StackOverflow here上。)
// Convert function for the MultiBinding
private object Convert(object[] values, Type targetType, object parameter, Culture culture){
var rawChildBindingResult = values[0]; // assuming it's in the first position
var convertedChildBindingResult = childConverter(rawChildBindingResult, targetType, childConverterParameter, culture);
if(convertedChildBindingResult == null)
convertedChildBindingResult = childTargetNullValue;
else if(targetType == typeof(string) && childStringFormat != null)
convertedChildBindingResult = string.Format(childStringFormat, convertedChildBindingResult);
// Now do whatever you would with the 'convertedChildBindingResult' as if things were normal
}
再次,看一下链接,在上下文中查看它。
希望这可以帮助!