0

ドロップダウンとグリッドがあります。グリッド列にハイパーリンクがあります。ドロップダウン コレクションに 1 つの項目が含まれている場合、ドロップダウンは非表示にする必要があります。つまり、項目のコレクションに対して可視性をバインドするロジックが必要です。

GRid 内の 1 つのハイパーリンクの 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();
    }
}
4

2 に答える 2

6

可視性をHasItemsプロパティにバインドし、BooleanToVisibilityConverterを使用できます

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

またはこのようなことをする

于 2012-12-04T12:02:17.777 に答える
1
  1. IsVisible は TextBlock のプロパティではありません。Visibilityプロパティを使用する必要があります。
  2. RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DPA2:TakenCoursesNotApplied}}"-タイプが TextBlock の上のビジュアル ツリーにあるコントロールでFindAncestorない限り、ステートメントを使用できません。DPA2:TakenCoursesNotApplied

おそらく、コレクション内の要素の数を受け入れVisibility.Collapsed、count == 1 の場合に返す値コンバーターが必要です。

于 2012-12-04T11:57:29.413 に答える