2015-01-21 128 views
1

我有這樣的事情:高級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 - >展控制。否則就隱藏它。

我想添加一些功能,即條件:

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

因此,如果otherButton被選中,隱藏控制(獨立於其他條件)。我希望能夠設置比1更多的「otherButtons」(如果至少有一個「otherButtons」被選中 - > Hide)。

回答

2

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

例如:

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

你不應該提供按鈕作爲綁定的值,因爲這種綁定永遠不會聽按鈕的屬性。 – 2015-01-21 09:48:06

1

試試這個:

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> 

這樣做是爲了告訴轉換器多少按鍵顯示了控制。如果此計數不是一個常數,則可以重構轉換器以將計數作爲第一次綁定。