试图根据 ComboBox 的 SelectedItem 绑定 GroupBox 的 Visibility 属性。我的组合框显示FreqTypeCollection
,我想隐藏/显示相应的 GroupBox。
public Dictionary<FreqType, string> FreqTypeCollection
{
get
{
var d =
new Dictionary<FreqType, string>()
{
{FreqType.OneTimeOnly, "One time only"},
{FreqType.Daily, "Recurring - Daily"},
{FreqType.Weekly, "Recurring - Weekly"},
{FreqType.Monthly, "Recurring - Monthly"},
{FreqType.MonthlyRelativeToFreqInterval, "Monthly relative"}
};
return d;
}
}
<ComboBox Name="ScheduleTypeComboBox" ItemsSource="{Binding Path=FreqTypeCollection}" DisplayMemberPath="Value"
SelectedValuePath="Key" SelectedValue="{Binding Path=freq_type, Mode=TwoWay, Converter={StaticResource FreqTypeConverter}}">
<GroupBox Header="One time only" Name="OneTimeOnlyGroupBox" Visibility="Collapsed">
<GroupBox.Style>
<Style TargetType="GroupBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=freq_type}" Value="1">
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</GroupBox.Style>
</GroupBox>
<GroupBox Header="Recurring-Daily" Name="DailyGroupBox" Visibility="Collapsed">
<GroupBox.Style>
<Style TargetType="GroupBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=freq_type}" Value="2">
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</GroupBox.Style>
</GroupBox>
class FreqTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
else
{
FreqType f = (FreqType)value;
return f;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int rValue=(int)value;
return rValue;
}
}
public enum FreqType
{
[Display(Name = "One time only")]
[Description("Task will be scheduled one time only.")]
OneTimeOnly = 1,
[Display(Name = "Daily")]
[Description("Recurring daily.")]
Daily = 4,
[Display(Name = "Weekly")]
[Description("Recurring weekly.")]
Weekly = 8,
[Display(Name = "Monthly")]
[Description("Recurring monthly.")]
Monthly = 16,
[Display(Name = "Monthly relative")]
[Description("Recurring monthly relatively.")]
MonthlyRelativeToFreqInterval = 32,
};