您可以使用MultiBinding
與轉換器:
<SomeControl>
<SomeControl.Tag>
<MultiBinding Converter="{StaticResource pairConverter}">
<Binding Path="A" />
<Binding Path="B" />
</MultiBinding>
</SomeControl.Tag>
</SomeControl>
使用此轉換器:
public class PairConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length == 2)
{
return new Pair { A = (string)values[0], B = (string)values[1] };
}
return Binding.DoNothing;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
Pair p = (Pair)value;
return new object[] { p.A, p.B };
}
}