Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to boolean && two visibility converters

I have two separate converters for visibility, one based on whether a field has been updated and one based on whether a field is allowed to be seen. I use the updatedField one for each text item on my page so that a star shows up next to an updated field. But some text items only are visible to some users based on permission levels.

For example:

<Image Visibility="{Binding ElementName=MyObject, Path=UpdatedFields, Mode=OneWay, Converter={StaticResource updatedFieldConverter}, ConverterParameter=FieldToTest}" Source="Properties:Resources.star_yellow" /> 

and

<TextBlock FontSize="21" Foreground="{DynamicResource LabelBrush}" Text="{x:Static Properties:Resources.Some_Text}" Visibility="{Binding Source={StaticResource allowedFields}, Path=Some_Text_Field, Converter={StaticResource visibilityConverter}}" /> 

My problem is that for the case of the permission-required fields I need to run both converters to determine if the star shows up. Is there a way to do a boolean "And" on the results of two converters?

I looked at this post but it doesn't seem to allow for different sets of parameters to be passed into to the two different converters.

-------Update--------

I also tried to create a MultiValueConverter with this xaml

<Image Grid.Row="4" Grid.Column="0" Source="star_yellow.png">    <Image.Visibility>       <MultiBinding Converter="{StaticResource combinedVisibilityConverter}" ConverterParameter="FieldToTest" >          <Binding ElementName="allowedFieldsModel" Path="Some_Text_Field" Mode="OneWay" />                                  <Binding ElementName="MyObject" Path="UpdatedFields" Mode="OneWay" />       </MultiBinding>    </Image.Visibility> </Image> 

But when it enters the converter both values are "DependencyProperty.UnsetValue". So I'm apparently doing something wrong here.

--------Solution---------

I had to modify to this, but then it worked.

<Image.Visibility>     <MultiBinding Converter="{StaticResource combinedVisibilityConverter}" ConverterParameter="FieldToTest">         <Binding Source="{StaticResource allowedFieldsModel}" Path="Some_Text_Field" />         <Binding Path="MyObject.UpdatedFields" />     </MultiBinding> </Image.Visibility> 
like image 757
Bill Avatar asked Jun 28 '11 05:06

Bill


1 Answers

You could use a MultiBinding together with a short, hand made IMultiValueConverter.

Example:

<StackPanel>     <StackPanel.Resources>         <local:MultiBooleanToVisibilityConverter x:Key="Converter" />     </StackPanel.Resources>     <CheckBox x:Name="Box1" />     <CheckBox x:Name="Box2" />     <TextBlock Text="Hidden Text">         <TextBlock.Visibility>             <MultiBinding Converter="{StaticResource Converter}">                 <Binding ElementName="Box1"                             Path="IsChecked" />                 <Binding ElementName="Box2"                             Path="IsChecked" />             </MultiBinding>         </TextBlock.Visibility>     </TextBlock>                    </StackPanel> 

... and the converter ...

class MultiBooleanToVisibilityConverter : IMultiValueConverter {     public object Convert(object[] values,                             Type targetType,                             object parameter,                             System.Globalization.CultureInfo culture)     {         bool visible = true;         foreach (object value in values)             if (value is bool)                 visible = visible && (bool)value;          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();     } } 
like image 151
Jens Avatar answered Sep 23 '22 04:09

Jens