我有下拉列表和网格。在网格列中有超链接。如果Dropdown集合包含1个项目,则下拉列表应该是不可见的。我需要一个逻辑来将Visibility绑定到项目集合。
以下是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();
}
}
答案 0 :(得分:5)
您可以将可见性绑定到HasItems属性并使用BooleanToVisibilityConverter
Visibility="{Binding HasItems,ElementName=ELementName,Converter={StaticResource BooleanToVisibilityConverter}"
答案 1 :(得分:1)
RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DPA2:TakenCoursesNotApplied}}"
- 您不能使用FindAncestor
语句,除非您的DPA2:TakenCoursesNotApplied
类型是TextBlock上方可视树中的控件。也许你需要一些接受你的集合中元素数量的值转换器,如果count == 1则返回Visibility.Collapsed
。