无法在 WPF MultiValueConverter 中转换对象
Unable to cast object in WPF MultiValueConverter
我正在努力学习 MultiBinding
。
我尝试将两个值(一个 double
和一个 boolean
)从视图模型传递给转换器,然后 return 返回一个 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; }
}
异常是由于 DataContext.SelectedCar.IsPowerAvailable
无法到达,请确保 SelectedCar
属性 确实可以从 ScrollViewer
DataContext 到达,这里是一个工作示例
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;
}
}
我正在努力学习 MultiBinding
。
我尝试将两个值(一个 double
和一个 boolean
)从视图模型传递给转换器,然后 return 返回一个 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; }
}
异常是由于 DataContext.SelectedCar.IsPowerAvailable
无法到达,请确保 SelectedCar
属性 确实可以从 ScrollViewer
DataContext 到达,这里是一个工作示例
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;
}
}