0

I have dropdown and Grid. in the grid column there is hyperlink.If the Dropdown collection contains 1 item then the dropdown should be invisible.I meean need a logic to bind Visibility against the collection of items.

Here is the XAML for one Hyperlink in the GRid.

   <!--Associate-->
                    <TextBlock Margin="10, 0, 0, 0">                                    
                        <TextBlock.Visibility>                                           
                                 <MultiBinding Converter="{StaticResource courseListVisibilityConverter}"
                                                     ConverterParameter="Associate">
                                          <Binding Path="IsCourseAssocited"
                                                   RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DPA2:TakenCoursesNotApplied}}" />
                                            <Binding Path="DataContext"
                                                     RelativeSource="{RelativeSource Self}" />
                                       </MultiBinding>
                            </TextBlock.Visibility>

                        <Hyperlink DataContext="{Binding}"
                                   Name="Associate"
                                    IsEnabled="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TextBlock}}, Path=IsVisible}"
                                    Click="Associate_Click">
                            <TextBlock TextWrapping="Wrap"
                                        Text="Associate" />
                        </Hyperlink>
                    </TextBlock>

 public class CourseListVisibilityConverter : IMultiValueConverter
{
    public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || value[0] == null || value[1] == null) return Visibility.Collapsed;
        bool IsEditMode = value[0] == DependencyProperty.UnsetValue ? false : (bool)value[0];

        if (value[1].GetType() == typeof(Course))
        {
            Course course = value[1] as Course;
            if (course == null) return string.Empty;

            //Visibility retVal = Visibility.Visible;
            switch (parameter.ToString())
            {
                case "CodeLink":
                    if (!IsEditMode) return Visibility.Collapsed;
                    if (course.HideOptions) return Visibility.Collapsed;
                    if (course.IsInElectivePool &&
                        course.IsManualAdd) return Visibility.Visible;
                    if (!course.IsInElectivePool &&
                            !course.IsWaived) return Visibility.Visible;

                    if (course.IsInElectivePool &&
                        course.FulFillmentId != 0 &&
                        course.FulFillmentId != -1 &&
                        !course.IsWaived) return Visibility.Visible;

                    return Visibility.Collapsed;

                case "CodeNoLink":
                    if (!IsEditMode) return Visibility.Visible;
                    if (course.HideOptions) return Visibility.Visible;

                    if (course.IsInElectivePool &&
                        course.IsManualAdd) return Visibility.Collapsed;
                    if (!course.IsInElectivePool &&
                          !course.IsWaived) return Visibility.Collapsed;

                    if (course.IsInElectivePool &&
                        course.FulFillmentId != 0 &&
                        course.FulFillmentId != -1 &&
                        !course.IsWaived) return Visibility.Collapsed;

                    return Visibility.Visible;

                case "Waive":
                    if (course.HideOptions) return Visibility.Collapsed;
                    if (IsEditMode &&
                        !course.IsWaived &&
                         !(course.RetakenFlag == RetakeFlagType.RetakeOfCourse) &&
                        !course.IsInElectivePool) return Visibility.Visible;
                    return Visibility.Collapsed;

                case "UndoWaive":
                    if (course.HideOptions) return Visibility.Collapsed;
                    if (IsEditMode && course.IsWaived &&
                          !(course.RetakenFlag == RetakeFlagType.RetakeOfCourse) &&
                        !course.IsInElectivePool) return Visibility.Visible;
                    return Visibility.Collapsed;

                case "UndoAdd":
                    if (course.HideOptions) return Visibility.Collapsed;
                    if (IsEditMode && course.IsManualAdd &&
                        (!(course.RetakenFlag == RetakeFlagType.RetakeOfCourse) &&
                         !course.IsInElectivePool)) return Visibility.Visible;
                    return Visibility.Collapsed;
                case "Associate":
                    if (course.HideOptions) return Visibility.Collapsed;
                    if (IsEditMode && course.IsCourseAssocited)
                        return Visibility.Collapsed;
                    else if (IsEditMode &&
                        ((course.Status.ToUpper() == "COMPLETE" || course.Status.ToUpper() == "DROPPED")))
                    {
                        return Visibility.Visible;
                    }
                    else
                    {
                        return Visibility.Collapsed;
                    }
                //case "UnAssociate":
                //    if (course.HideOptions) return Visibility.Collapsed;
                //    if (IsEditMode && (course.IsCourseAssocited))

                //    {
                //        return Visibility.Visible;
                //    }
                //    else
                //    {
                //        return Visibility.Collapsed;
                //    }
                case "FullFillRequirement":
                    if (course.HideOptions) return Visibility.Collapsed;
                    if (IsEditMode &&
                        ((course.Status.ToUpper() == "FUTURE")))
                    {
                        return Visibility.Visible;
                    }
                    else
                    {
                        return Visibility.Collapsed;
                    }
                default:
                    return Visibility.Collapsed;
            }
        }
        else if (value[1].GetType() == typeof(ElectivePool))
        {
            ElectivePool electivePool = value[1] as ElectivePool;
            if (electivePool == null) return string.Empty;

            switch (parameter.ToString())
            {
                case "Waive":

                    if (IsEditMode &&
                        !electivePool.IsWaived) return Visibility.Visible;
                    return Visibility.Collapsed;

                case "UndoWaive":
                    if (IsEditMode &&
                        electivePool.IsWaived) return Visibility.Visible;
                    return Visibility.Collapsed;
            }
        }
        return Visibility.Visible;
    }

    public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
KCS
  • 29
  • 2
  • 6

2 Answers2

6

You could bind visibility to the HasItems property and use BooleanToVisibilityConverter

Visibility="{Binding HasItems,ElementName=ELementName,Converter={StaticResource BooleanToVisibilityConverter}"

or do something like this or this

Community
  • 1
  • 1
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
  • This works like a charm, but only thing is, immediately if I remove the items from the list the visibility of the textBlock should be visible again, that's not happening, – Debhere Oct 08 '13 at 04:50
1
  1. IsVisible is not a property of TextBlock, you need to use Visibility property;
  2. RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DPA2:TakenCoursesNotApplied}}" - You can't use FindAncestor statement unless your DPA2:TakenCoursesNotApplied type is control that is in visual tree above your TextBlock.

Perhaps you need some value converter that accepts count of elements in your collection and returns Visibility.Collapsed if count == 1.

EugenSoft
  • 536
  • 4
  • 13
  • ,can we use A collection which is in Presentation model in the converter. – KCS Dec 04 '12 at 12:25
  • 2.Can we use property which is poplated in the PresentationModel, in the converter.I mean to say In model i check the collection count and update boolena property.Can i use the same property in the Converter calss. – KCS Dec 04 '12 at 12:27
  • Yes, you can use your boolean property with [BooleanToVisibilityConverter](http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx), but not forget to use PropertyChanged notification. [Here](http://msdn.microsoft.com/en-us/library/ms743695.aspx) is example. – EugenSoft Dec 04 '12 at 12:31
  • You can use something like `` where "CollectionContainsMoreThanOneElement" is your boolean property and booleanToVisibilityConverter is instance of [BooleanToVisibilityConverter](http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx). – EugenSoft Dec 04 '12 at 13:15
  • This visibility will work for the ComoboBox.But i need the visibility of Hyperlink.I ahave added the Converter code.but can i aceess the model property in the converter.so that i can add a extra check here. – KCS Dec 04 '12 at 13:27
  • Yes, and you don't need MultiValueConverter, you can use simple IValueConverter implementation in your CourseListVisibilityConverter and use this converter for TextBlock Visivility like this: `` then you will have in your converter whole your PresentationModel and you'll can add any extra checks that you need in your CourseListVisibilityConverter. – EugenSoft Dec 04 '12 at 13:47