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 にバインドする必要があります (この場合、トリガーはコンボボックスで新しく選択された項目をどのように認識しますか?)

    Binding="{Binding Path=SelectedIndex, ElementName=ScheduleTypeComboBox}"

  2. トリガーには、プロパティのデフォルト値のセッターが必要です

    Setter プロパティ="Visibility" 値="Collapsed"

それはすべてコードです:

 <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 に答える