簡體   English   中英

先進的多重裝訂

[英]Advanced multibinding

我有這樣的事情:

<Controls:ToggleRectangleButton.Visibility>
   <MultiBinding Converter="{StaticResource MultiButtonCheckedToVisibilityConverter}">
      <Binding ElementName="btDayAndNightsLinesTickets" Path="IsButtonChecked" />
      <Binding ElementName="btSchoolSemester" Path="IsButtonChecked" />
   </MultiBinding>
</Controls:ToggleRectangleButton.Visibility>

MultiButtonCheckedToButtonEnabledConverter的轉換方法

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    bool visible = false;
    foreach (object value in values)
    {
        if (value is bool)
        {
            if ((bool)value == true) visible = true;
        }
    }   
    if (visible)
    {
        return System.Windows.Visibility.Visible;
    }
    else
    {
        return System.Windows.Visibility.Hidden;
    }
}

因此,這意味着如果作為參數傳遞的按鈕中至少有一個具有IsButtonChecked屬性設置為true-> show control。 否則將其隱藏。

我要添加一些功能,這是條件:

如果(otherButton.IsChecked)返回System.Windows.Visibility.Hidden;

因此,如果選中otherButton,則隱藏控件(獨立於其他條件)。 我希望能夠設置多於1個的“ otherButtons”(如果選中了“ otherButtons”中的至少一個->隱藏)。

嘗試這個:

public class MultiButtonCheckedToVisibilityConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool visible = false;
        int trueCount = (int)parameter;

        for (int i = 0; i < trueCount; i++)
        {
            if ((bool)values[i]) 
            {
                visible = true;
                break;
            }
        }

        if (visible)
        {
            for (int i = trueCount; i < values.Length; i++)
            {
                if (!(bool)values[i])
                {
                    visible = false;
                    break;
                }
            }
        }

        if (visible)
        {
            return System.Windows.Visibility.Visible;
        }
        else
        {
            return System.Windows.Visibility.Hidden;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

<Button Content="Test">
    <Button.Visibility>
        <MultiBinding Converter="{StaticResource MultiButtonCheckedToVisibilityConverter}">
            <MultiBinding.ConverterParameter>
                <sys:Int32>2</sys:Int32>
            </MultiBinding.ConverterParameter>
            <Binding ElementName="btDayAndNightsLinesTickets" Path="IsChecked" />
            <Binding ElementName="btSchoolSemester" Path="IsChecked" />
            <Binding ElementName="btOther1" Path="IsChecked" />
            <Binding ElementName="btOther2" Path="IsChecked" />
        </MultiBinding>
    </Button.Visibility>
</Button>
<ToggleButton Name="btDayAndNightsLinesTickets">btDayAndNightsLinesTickets</ToggleButton>
<ToggleButton Name="btSchoolSemester">btSchoolSemester</ToggleButton>
<ToggleButton Name="btOther1">btOther1</ToggleButton>
<ToggleButton Name="btOther2">btOther2</ToggleButton>

這個想法是告訴轉換器多少按鈕顯示控件。 如果此計數不是常數,則可以重構轉換器以將計數作為第一個綁定接收。

綁定的順序將保留在轉換器代碼中。 您可以使用索引檢查object[] values ,並根據它實現邏輯。

例如 :

if((values[0] is bool) && ((bool)values[0]))
{
    //DoSomething
}

暫無
暫無

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

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