MultiBinding和IMultiValueConverter Convert()只调用一次

时间:2010-08-24 13:33:16

标签: c# wpf user-controls binding

问:为什么使用MultiBinding和IMultiValueConverter的自定义TextBox UserControl只能调用一次Convert()方法(在实例化期间)??

我已经定义了一个需要MultiBindingIMultiValueConverter的UserControl,以便在2个indenpendant DependencyProperty上更改其行为/演示文稿。

<proj:MyControl Value="10" Digits="1" />

用户控件:

<UserControl x:Class="MyControl"
             x:Name="uc"
             ...>

    <UserControl.Resources>
        <conv:DecimalToStringMultiConverter x:Key="DecToString" />
    </UserControl.Resources>

    [...]

    <Grid>
        <ctrl:VTextBox x:Name="vTb" Grid.Column="0" Margin="0,0,2,0">
            <ctrl:VTextBox.Text>
                <MultiBinding Converter="{StaticResource DecToString}" UpdateSourceTrigger="LostFocus" Mode="TwoWay">
                    <Binding ElementName="uc" Path="Value" Mode="TwoWay" />
                    <Binding ElementName="uc" Path="Digits" Mode="TwoWay" />
                </MultiBinding>
            </ctrl:VTextBox.Text>
        </ctrl:VTextBox>
    </Grid>
</UserControl>

执行应用程序时,UserControls都已正确实例化。但是,IMultiValueConverter.Convert()方法只被称为一次

使用带有常量Binding的简单IValueConverter + ConvertParameter效果很好:每当Convert()包含在TextBox内时,就会调用转换器的UserControl方法{1}}更改了Text属性。

设计已更改,我不得不使用MultiBinding + IMultiValueConverter,现在Convert()方法只调用一次,TextBox.Text属性永远不会更新在LostFocus()事件。

是什么给出了?

MultiValueConverter定义如下。我只是在IValueConverter上包装IMultiValueConverter以重用现有代码。

[ValueConversion(/*sourceType*/ typeof(Decimal), /*targetType*/ typeof(string))]
public class DecimalToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return "0.00";

        int? digits = parameter as int?;

        if(digits == null)
            digits = 2;

        NumberFormatInfo nfi = (NumberFormatInfo) CultureInfo.InvariantCulture.NumberFormat.Clone();
        nfi.NumberGroupSeparator = " ";
        nfi.CurrencyDecimalSeparator = ".";
        nfi.NumberDecimalDigits = (int)digits;

        return ((decimal)value).ToString("n", nfi);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return 0.00m;

        decimal d;

        return decimal.TryParse((string)value, out d) ? d : 0.00m;
    }
}

[ValueConversion(/*sourceType*/ typeof(Decimal), /*targetType*/ typeof(string))]
public class DecimalToStringMultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        DecimalToStringConverter conv = new DecimalToStringConverter();
        return conv.Convert(values[0], targetType, values.Length > 1 ? values[1] : null, culture);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        DecimalToStringConverter conv = new DecimalToStringConverter();
        return new[] { conv.ConvertBack(value, targetTypes[0], null, culture) };
    }
}

1 个答案:

答案 0 :(得分:0)

看起来你对Binding和TextBox的更新行为有一些相互矛盾的期望。转换将被多次调用的唯一原因是,如果Digits或Value的值多次更改,并且您的已发布代码中没有任何内容表明将会发生这种情况。对TextBox.Text的更改不会导致对Convert的调用,而应该在每次更改+ LostFocus上调用 ConvertBack 。你在运行代码时看到了吗?

您还需要从ConvertBack方法返回两个值,而不是现在的值,以便为MultiBinding中使用的两个Binding提供值。