我有这样的事情:
<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 - &gt;显示控制。否则隐藏它。
我想添加一些功能,即条件:
if(otherButton.IsChecked)返回System.Windows.Visibility.Hidden;
因此,如果检查了otherButton,则隐藏控件(独立于其他条件)。我希望能够设置比“1更多”的“otherButtons”(如果选中“otherButtons”中的至少一个 - >&gt;隐藏)。
答案 0 :(得分: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>
这个想法是告诉转换器有多少按钮显示控件。如果此计数不是常量,则可以重构转换器以将计数作为第一个绑定接收。
答案 1 :(得分:0)
绑定的顺序将保留在转换器代码中。
您可以使用索引检查object[] values
并根据它实现您的逻辑。
例如:
if((values[0] is bool) && ((bool)values[0]))
{
//DoSomething
}