0

试图根据 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,
};  
4

1 回答 1

1

你在 xaml 中有一些错误

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

    绑定="{绑定路径=SelectedIndex, ElementName=ScheduleTypeComboBox}"

  2. 触发器应该为您的属性设置默认值

    设置器属性=“可见性”值=“折叠”

都是代码:

 <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>
于 2013-11-05T08:14:33.040 回答