无法在WPF MultiValueConverter中转换对象

时间:2016-01-25 09:21:34

标签: c# wpf imultivalueconverter

我正在努力学习MultiBinding。 我尝试将两个值(一个double和一个boolean)从viewmodel传递到转换器并返回string结果。

XAML

<TextBlock>
     <TextBlock.Text>
         <MultiBinding Converter="{StaticResource MyValueConverter}">
              <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}" Path="DataContext.SelectedCar.PowerValue" />
              <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}" Path="DataContext.SelectedCar.IsPowerAvailable" />
         </MultiBinding>
     </TextBlock.Text>
</TextBlock>

转换器

class MyValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (System.Convert.ToBoolean(values[1]))
        {
            return string.Format("{0:n1}", values[0]); 
        }
        else
        {
            return "[not available]";
        }
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

视图模型

public double PowerValue
public bool IsPowerAvailable

在转换器中,我将获得异常Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.IConvertible

这是什么意思?我是否错误地实施了MultiBinding

修改

ViewModel看起来像这样

class CarViewModel : ViewModelBase
{
    private car _selectedCar;
    public car SelectedCar
    {
        get { return _selectedCar; }
        set
        {
            _selectedCar = value;
        }
    }

模型看起来像这样:

class car 
{
    public double PowerValue { get; set; }
    public bool IsPowerAvailable{ get; set; }
}

1 个答案:

答案 0 :(得分:0)

异常是由于无法访问DataContext.SelectedCar.IsPowerAvailable这一事实,请确保SelectedCar DataContext确实可以访问ScrollViewer属性,此处为工作示例< / p>

 Title="MainWindow" Height="350" Width="525" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <wpfApplication12:MyValueConverter x:Key="MyValueConverter"/>
</Window.Resources>
<Grid>      
  <ScrollViewer>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource MyValueConverter}">
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}" Path="DataContext.SelectedCar.PowerValue" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}" Path="DataContext.SelectedCar.IsPowerAvailable" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </ScrollViewer>
</Grid>

并且selectedCar在VM / CodeBhind中定义:

private Car _selectedCar = new Car()
    {
        IsPowerAvailable = true,
        PowerValue = 14
    };
    public Car SelectedCar
    {
        get
        {
            return _selectedCar;
        }

        set
        {
            if (_selectedCar == value)
            {
                return;
            }

            _selectedCar = value;                
        }
    }