尝试将ComboBox SelectedItem绑定到GroupBox可见性属性

时间:2013-11-05 05:06:57

标签: wpf

尝试基于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,
};  

1 个答案:

答案 0 :(得分:0)

你在xaml中有一些错误

  1. 触发器应该绑定到您的组合框的SelectedIndex(在您的情况下,触发器如何知道组合框中的新选项?)

    Binding =“{Binding Path = SelectedIndex,ElementName = ScheduleTypeComboBox}”

  2. 触发器应该具有默认值的setter属性

    Setter Property =“Visibility”Value =“Collapsed”

  3. 这是所有代码:

     <ComboBox x: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" x:Name="OneTimeOnlyGroupBox">
                        <GroupBox.Style>
                            <Style TargetType="GroupBox">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=SelectedIndex, ElementName=ScheduleTypeComboBox}" Value="0">
                                        <Setter Property="Visibility" Value="Visible"></Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </GroupBox.Style>
                    </GroupBox>
                    <GroupBox Header="Recurring-Daily" x:Name="DailyGroupBox">
                        <GroupBox.Style>
                            <Style TargetType="GroupBox">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=SelectedItem, ElementName=ScheduleTypeComboBox}" Value="1">
                                        <Setter Property="Visibility" Value="Visible"></Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </GroupBox.Style>
                    </GroupBox>