1

MultiBinding转换器的问题
我知道两次发送相同的值没有意义
我这样做是为了隔离那个单一的绑定
在转换器中我收到一个错误转换为bool
值是

{DependencyProperty.UnsetValue}

但是“RWnet”是一个布尔值

<ContentPresenter Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
        x:Name="fieldTemplateDetail"
        Content="{Binding}"
        ContentTemplateSelector="{StaticResource fieldTemplateSelector}">
    <ContentPresenter.Visibility>
        <MultiBinding Converter="{StaticResource visabilityConverterTwoBool}">
            <Binding Path="RWnet" />
            <Binding Path="RWnet" />
        </MultiBinding>
    </ContentPresenter.Visibility>
</ContentPresenter>

public class VisabilityConverterTwoBool : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == null) return Visibility.Visible;
        if (values.Length != 2) return Visibility.Visible;
        try
        {
            if ((bool)values[0] == false) return Visibility.Collapsed;
            if ((bool)values[1] == false) return Visibility.Collapsed;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message, "VisabilityConverterTwoBool");
            Debug.WriteLine(values[0].ToString());
            Debug.WriteLine(values[1].ToString());
        }
        return Visibility.Visible;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}

如果我发送实际 Multi Binding 的另一半,它可以工作(错误的结果但没有错误)

<Binding ElementName="cbEditMode" Path="IsChecked" />

但这很好
用 RWnet 是一个布尔值

<ContentPresenter Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
        Visibility="{Binding Path=RWnet, Converter={StaticResource bvc}}"
        x:Name="fieldTemplateDetail"
        Content="{Binding}"
        ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>

<BooleanToVisibilityConverter x:Key="bvc" />

对于 McGarnagle,这是数据上下文

<ListBox  Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" x:Name="lbCurDocFields" 
            ItemsSource="{Binding Source={StaticResource curDocFields}}" LostFocus="lbCurDocFields_LostFocus"
4

1 回答 1

1

根据您连接数据上下文的方式,不能保证绑定会在您期望的时候准备好。完全验证输入,它应该可以解决问题:

if (values.Length != 2 && values[0] as bool? == null || values[1] as bool? == null)
    return Visibility.Visible;
于 2014-02-06T23:38:06.653 回答