2013-10-22 58 views
2

我對MVVM相當新,所以如果問題不清楚,請與我聯繫,讓我知道,我會澄清。Multibinding生成「不能設置MultiBinding,因爲MultiValueConverter必須指定」

我有約束力的正常工作按鈕,見下圖:

<Button x:Name="licenceFilterSet" Content="Search" Command="{Binding searchCommand}" CommandParameter="{Binding Path=Text, ElementName=licenseTextBox}" > 

現在我已經意識到我需要的信息,另一個和平,所以我需要發送check-box的價值也是如此。 我修改了VM是這樣的:

<Button x:Name="licenceFilterSet" Content="Search" Command="{Binding licenseSearchCommand}"> 
    <Button.CommandParameter> 
     <MultiBinding Converter="{StaticResource searchFilterConverter}"> 
      <Binding Path="Text" ElementName="licenseTextBox" /> 
      <Binding Path="IsEnabled" ElementName="regularExpressionCheckBox" /> 
     </MultiBinding> 
    </Button.CommandParameter> 
</Button> 

下面是我的多轉換器:

/// <summary> 
/// Converter Used for combining license search textbox and checkbox 
/// </summary> 
public class SearchFilterConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values) 
    { 
     return new Tuple<String, bool>((String)values[0], (bool)values[1]);; 
    } 
} 

我在做什麼錯。我收到以下錯誤,(這是指着我的MultiBinding標籤在axml):

Cannot set MultiBinding because MultiValueConverter must be specified. 

回答

4

你必須實現IMultiConverter

public class SearchFilterConverter : IMultiValueConverter 
{ 
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
{ 
    return new Tuple<String, bool>((String)values[0], (bool)values[1]);; 
} 
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

然後創建XAML資源

<Converter:SearchFilterConverter x:Key="searchFilterConverter" /> 

那麼它應該工作

<Button x:Name="licenceFilterSet" Content="Search" Command="{Binding licenseSearchCommand}"> 
<Button.CommandParameter> 
    <MultiBinding Converter="{StaticResource searchFilterConverter}"> 
     <Binding Path="Text" ElementName="licenseTextBox" /> 
     <Binding Path="IsEnabled" ElementName="regularExpressionCheckBox" /> 
    </MultiBinding> 
</Button.CommandParameter> 
</Button> 
+0

我設法找到了答案,但你點上:) – theAlse

+0

呵呵。我的代碼已經像這樣構建並構建並運行良好,但是這個錯誤仍然出現在錯誤列表(VS2015)中。從實驗上看,這個問題似乎是我的IMultiValueConverter也是一個MarkupExtension,這會讓解析器感到困惑。 – dlf

+0

@dlf你有沒有想過解決這個問題的方法? – claudekennilol

0

這是不正確執行IMultiValueConverter接口。

正確的是:

public class SearchFilterConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     .... 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
    } 
} 

參考here