2012-12-04 90 views
7

我只是試圖將兩個控件綁定爲命令參數,並將它們作爲object[]傳遞到我的命令中。WPF CommandParameter MultiBinding值null

XAML:

<UserControl.Resources> 
     <C:MultiValueConverter x:Key="MultiParamConverter"></C:MultiValueConverter> 
    </UserControl.Resources> 

    <StackPanel Orientation="Vertical"> 
     <StackPanel Orientation="Horizontal"> 
      <Button Name="Expander" Content="+" Width="25" Margin="4,0,4,0" Command="{Binding ExpanderCommand}"> 
       <Button.CommandParameter> 
        <MultiBinding Converter="{StaticResource MultiParamConverter}"> 
         <Binding ElementName="Content"/> 
         <Binding ElementName="Expander"/> 
        </MultiBinding> 
       </Button.CommandParameter> 
      </Button> 
      <Label FontWeight="Bold">GENERAL INFORMATION</Label> 
     </StackPanel> 
     <StackPanel Name="Content" Orientation="Vertical" Visibility="Collapsed"> 
      <Label>Test</Label> 
     </StackPanel> 
    </StackPanel> 

命令:

public ICommand ExpanderCommand 
     { 
      get 
      { 
       return new RelayCommand(delegate(object param) 
        { 
         var args = (object[])param; 
         var content = (UIElement)args[0]; 
         var button = (Button)args[1]; 
         content.Visibility = (content.Visibility == Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible; 
         button.Content = (content.Visibility == Visibility.Visible) ? "-" : "+"; 
        }); 
      } 
     } 

轉換器:

public class MultiValueConverter : IMultiValueConverter 
    { 
     public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return values; 
     } 

     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException("No two way conversion, one way binding only."); 
     } 
    } 

基本上正在發生的事情是,結合似乎是工作的罰款和轉換器返回一個object[]包含正確的值,但是當命令執行段時m是包含相同數量元素的object[],除了它們是null

有人可以告訴我爲什麼object[]參數的值設置爲null

謝謝, Alex。

回答

15

這會做到這一點:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
{ 
    return values.ToArray(); 
} 

看看這個question的解釋。