簡體   English   中英

WPF IValueConverter - 將多個值轉換為單個值

[英]WPF IValueConverter - converting multiple values into a single value

我正在努力維護別人的代碼,因為那個人是WPF專家。 另一方面,我不是。 :)

該代碼使用IValueConverter將狀態枚舉轉換為布爾值,該布爾值控制是否在屏幕上顯示UserControl。

我發現了一個缺點,即在這種情況下單個枚舉是不夠的,實際上還需要考慮另一個布爾值。 是否有另一個可以使用的對象,它可以將2個項目作為參數進行轉換? (“converter”參數已被使用。)

一個簡單的例子如下。

現有代碼的邏輯說......

If it's sunny, go to work.
If it's raining, don't go to work.

我需要考慮另一件事情,如下所示。

If it's sunny and you're wearing pants, go to work.
If it's sunny and you're not wearing pants, don't go to work.
If it's raining and you're wearing pants, don't go to work.
If it's raining and you're not wearing pants, don't go to work.

執行轉換的IValueConverter只允許我將一個“東西”用於轉換。

任何幫助表示贊賞。 謝謝,

MJ

使用IMultiValueConverter

public class MyMultiValueConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        // Do something with the values array. It will contain your parameters
    }

    public object[] ConvertBack(object values, Type[] targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

您還需要在XAML中使用MultiBinding而不是常規綁定

<MultiBinding Converter="{StaticResource MyMultiValueConverterKey}">
    <Binding Path="Value1" />
    <Binding Path="Value2" />
</MultiBinding>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM