【问题标题】:RadioButton Not Checked on Start开始时未选中单选按钮
【发布时间】:2019-03-29 15:20:45
【问题描述】:

我有一个带有四个单选按钮的应用程序来选择我正在操作的模式。 所有 4 个单选按钮都绑定到同一个属性。当我启动程序时,没有选中任何单选按钮。

这是单选按钮的代码:

    <GroupBox Grid.Row="0" Margin="10,10,10,10" FontSize="16" 
    FontWeight="Bold">
        <GroupBox.Header>Tipo di Rientro</GroupBox.Header>
        <StackPanel>
            <RadioButton Name="RdBtnExternalEntry" FontSize="12" 
            FontWeight="Normal" GroupName="SelectionType" IsChecked="{Binding 
            Path=CurrentOption, Mode=TwoWay, Converter={StaticResource 
            enumConverter}, ConverterParameter=ExternalEntry}">Entrata da 
            Esterno</RadioButton>

            <RadioButton Name="RdBtnEntryAfterCheck" FontSize="12" 
            FontWeight="Normal" GroupName="SelectionType" IsChecked="{Binding 
            Path=CurrentOption, Mode=TwoWay, Converter={StaticResource 
            enumConverter}, ConverterParameter=EntryAfterCheck}">Rientro dopo 
            visione</RadioButton>

            <RadioButton Name="RdBtnEntryMissingShipping" FontSize="12" 
            FontWeight="Normal" GroupName="SelectionType" IsChecked="{Binding 
            Path=CurrentOption, Mode=TwoWay, Converter={StaticResource 
            enumConverter}, ConverterParameter=EntryMissingShipping}">Rientro 
            per mancata Spedizione</RadioButton>

            <RadioButton Name="RdBtnEntryAfterPicking" FontSize="12" 
            FontWeight="Normal" GroupName="SelectionType" IsChecked="{Binding 
            Path=CurrentOption, Mode=TwoWay, Converter={StaticResource 
            enumConverter}, ConverterParameter=EntryAfterPicking}">Rientro 
            dopo Picking</RadioButton>
        </StackPanel>
    </GroupBox>

这是属性:

public RadioOptions CurrentOption
        {
            get => _currentOption;
            set
            {
                _currentOption = value;
                NewLoadCommand.RaiseCanExecuteChanged();
                ConfirmCommand.RaiseCanExecuteChanged();
                if (value == RadioOptions.ExternalEntry)
                {
                    _selectedStockUnitCode = PalletToDo;
                    SelectedLoadnumber = LoadToDo;
                    RaisePropertyChanged("SelectedLoadnumber");
                    RaisePropertyChanged("SelectedStockUnitCode");
                }
                else
                {
                    SelectedLoadnumber = "0";
                    RaisePropertyChanged("SelectedLoadnumber");
                }

                RaisePropertyChanged("SelectedStockUnitCodeIsEnabled");
                RaisePropertyChanged("SelectedLoadnumberIsEnabled");
            }
        }

这是转换器:

public class EnumMatchToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return false;

            string checkValue = value.ToString();
            string targetValue = parameter.ToString();
            return checkValue.Equals(targetValue,
                     StringComparison.InvariantCultureIgnoreCase);
        }

        public object ConvertBack(object value, Type targetType,
                                  object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return null;

            bool useValue = (bool)value;
            string targetValue = parameter.ToString();
            if (useValue)
                return Enum.Parse(targetType, targetValue);

            return Binding.DoNothing;
        }

    }

这些是单选选项:

public enum RadioOptions { ExternalEntry, EntryAfterCheck, EntryMissingShipping, EntryAfterPicking }

我希望在程序开始时检查第一个组合框

【问题讨论】:

  • 为什么会这样?没有任何迹象表明该值已更改。
  • _currentoption 不可为空,因此它从枚举 RadioOptions 的第一个值开始。在开始时,程序应该使用 get 分支来更新单选按钮。
  • 你能发布你的RadioOptions吗?
  • 我尝试了您的代码并按照您的预期工作。如何在视图上设置 DataContext?

标签: c# wpf mvvm radio-button


【解决方案1】:

解决了!我是,错误地启动了我的窗口的另一个副本,因为它弄乱了 xaml 管理单选按钮的方式。我删除了其中一个,然后它就可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-04
    • 2023-04-09
    • 2014-08-30
    • 2016-06-17
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多