2012-12-04 2 views
7

나는 두 개의 컨트롤을 명령 매개 변수로 바인딩하고이를 내 명령에 object[]으로 전달하려고합니다.WPF CommandParameter MultiBinding values ​​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[] 올바른 값을 포함하지만 명령이 para를 실행할 때 m은 null을 제외하고 동일한 수의 요소를 포함하는 object[]입니다.

누군가 object[] 매개 변수의 값을 null으로 설정하는 이유를 알려주실 수 있습니까?

감사합니다. 알렉스.

답변

15

이 그것을 할 수 있습니다 :

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

는 설명이 question를 살펴 보자.

관련 문제