bew*_*mer 8 .net wpf mvvm relaycommand imultivalueconverter
我有以下代码:
<DataGridTemplateColumn Header="Security">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="Security" Content="{Binding Path=totalSecurities}" Command="{Binding Source={StaticResource viewModel}, Path=filterGridCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource PassThroughConverter}">
<Binding Path="sector"/>
<Binding ElementName="Security" Path="Name"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)
以下是PassThroughConverter的代码:
public class PassThroughConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameters, CultureInfo culture)
{
return values;
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
当我在命中返回值行时调试,正确的值在数组中,但是当我按下触发filtergrid命令的按钮时,返回的值都是null?谁能帮忙.谢谢.
Phi*_*hil 12
一些实验证实了这样做
public object Convert(object[] values, Type targetType,
object parameters, CultureInfo culture)
{
return values;
}
Run Code Online (Sandbox Code Playgroud)
导致命令参数结束为object[] { null, null }.
每次绑定值更改时都会运行转换器,而不是在执行命令时运行,并且在执行命令时缓存返回值以供使用.原始参数object[] values似乎重置为所有空值.
解决方案是克隆values参数.在你的情况下,你可以这样做:
public object Convert(object[] values, Type targetType,
object parameter, CultureInfo culture)
{
return new [] {values[0], values[1]};
}
Run Code Online (Sandbox Code Playgroud)
更有用的是,可以像这样处理可变数量的值:
public object Convert(object[] values, Type targetType,
object parameter, CultureInfo culture)
{
return values.ToArray();
}
Run Code Online (Sandbox Code Playgroud)