针对 WPF 中项集合的可见性



我有下拉列表和网格。 在网格列中有超链接。如果下拉列表集合包含 1 项,则下拉列表应不可见。我需要一个逻辑来将可见性绑定到项目集合。

下面是 GRid 中一个超链接的 XAML。

   <!--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();
    }
}

您可以将可见性绑定到 HasItems 属性并使用 BooleanToVisibilityConverter

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

或者做这样或这样的事情

  1. IsVisible 不是 TextBlock 的属性,需要使用 Visibility 属性;
  2. RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DPA2:TakenCoursesNotApplied}}" - 不能使用语句FindAncestor除非DPA2:TakenCoursesNotApplied类型是位于 TextBlock 上方的可视化树中的控件。

也许您需要一些值转换器,它接受集合中的元素计数,并在count == 1时返回Visibility.Collapsed

相关内容

  • 没有找到相关文章

最新更新