-1

I'm working on a new WPF form. I'm using the Extended.Wpf.Toolkit's MaskedTextBox. I'm using the IsMaskCompleted property in a MultiBinding expression. Here's the XAML:

<Border Grid.Row="1" 
        Grid.Column="1">
    <Border.BorderThickness>
        <MultiBinding Converter="{StaticResource multiBoolToThicknessConverter}">
            <Binding ElementName="TargetMaskedTextBox, Path=IsMaskCompleted" />
            <Binding Path="IsTargetCompleted" />
        </MultiBinding>
    </Border.BorderThickness>
    <tk:MaskedTextBox x:Name="TargetMaskedTextBox"
                      Style="{StaticResource LeftAlignMaskedTextBoxStyle}"
                      Value="{Binding Target}"
                      IsEnabled="{Binding IsTargetEnabled}"
                      ValueDataType="{x:Type System:Single}"
                      Mask="\0.0##" />
</Border>

and here's the Convert method in my IMultiValueConverter:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    try
    {
        bool? boolOneNullable = values[0] as bool?;
        bool? boolTwoNullable = values[1] as bool?;

        bool boolOne = true;
        bool boolTwo = true;

        if (!boolOneNullable.HasValue)
        {
            boolOne = false;
        }
        else
        {
            boolOne = boolOneNullable.Value;
        }

        if (!boolTwoNullable.HasValue)
        {
            boolTwo = false;
        }
        else
        {
            boolTwo = boolTwoNullable.Value;
        }

        var combinedBools =  boolOne && boolTwo;

        if (combinedBools)
        {
            return new Thickness(0);
        }
        else
        {
            return new Thickness(1);
        }

    }
    catch (Exception)
    {
        return null;
    }
}

What I don't understand is that when selecting a row in a datagrid, which populates the MaskedTextBox control, the IsMaskCompleted will often be null, even though there's valid data within it. Why is that?

Rod
  • 4,107
  • 12
  • 57
  • 81
  • Probably because the property hasn't been initialized by the time your `Convert` method is called. Is it called again shortly afterwards with an actual value? – mm8 Sep 09 '20 at 15:04
  • I believe it is. Our apps all start with a summary view of the data in a datagrid, on the first tab of a tab control. Then when a user double clicks on a row, they're sent to the second tab of the tab control, where they'll see a detailed view of the row they've double clicked on. It's on that second tab that I've got the MaskedTextBox control. – Rod Sep 09 '20 at 15:28
  • You didn't answer my question. – mm8 Sep 10 '20 at 14:07
  • Yes, mm8, it is called again very shortly afterwards with an actual value, fetched from the database. – Rod Sep 11 '20 at 04:05

1 Answers1

1

You cannot assume that the property has been initialized by the time your Convert method is called for the first time. It will be called again shortly afterwards with an actual value.

You should check whether the values are provied and simpy return of they aren't:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    if (values == null || values.Length < 2 || (values[0] == null && values[1] == null))
        return;
    ...
mm8
  • 163,881
  • 10
  • 57
  • 88