如何启动单选按钮 在 WPF MVVM 应用程序中选中 = true

本文关键字:应用程序 MVVM true WPF 何启动 启动 单选按钮 | 更新日期: 2023-09-27 18:31:53

嗨,基本上我有一个使用 MVVM 模式的 WPF 应用程序。

这是我的观点模型

namespace enLoja.WPF.ViewModel.Relatórios
{
    public class SEL_PG_C_ALIViewModel : ViewModelBase
    {
        private readonly ICAD_EF_C_ALIService _cadEfCAliService;
        //Commands
        public RelayCommand OnLoaded { get; set; }
        public RelayCommand Gerar { get; set; }
        public SEL_PG_C_ALIViewModel(ICAD_EF_C_ALIService cadEfCAliService)
        {
            _cadEfCAliService = cadEfCAliService;
            IsDataLoaded = false;
            OnLoaded = new RelayCommand(OnLoadedExecute);
            Gerar = new RelayCommand(GerarExecute, GerarCanExecute);
        }
        public async void Load()
        {
            await Task.Factory.StartNew(() =>
            {
                IsDataLoaded = true;
                RaisePropertyChanged("IsDataLoaded");
            });
        }
        public bool CodigoChecked { get; set; }
        public bool DescricaoChecked { get; set; }
        public bool IsDataLoaded { get; set; }
        #region Commands Execute
        public void OnLoadedExecute()
        {
            Load();
        }
        public void GerarExecute()
        {
            var parameters = new Dictionary<string, string>();
            if (CodigoChecked)
            {             
                parameters.Add("Order", "Código");
            }
            if (DescricaoChecked)
            {                
                parameters.Add("Order", "Descrição");
            }
            IEnumerable<CAD_EF_C_ALI> query = _cadEfCAliService.GetCAD_EF_C_ALI();
            var empresaSelecionada = new List<CAD_EF_C_PAR> { ((App)Application.Current).EmpresaSelecionada };
            var reportWindow = new REL_PG_C_ALI(query.ToList(), parameters, empresaSelecionada);
            reportWindow.ShowDialog();
        }
        public bool GerarCanExecute()
        {
            return (IsDataLoaded);
        }
        #endregion
    }
}

这是我的 xaml

<GroupBox x:Name="grbOrdenacao" Header="Ordenação"
                Grid.Column="1"                          
                Style="{StaticResource GroupBoxCadastro}" Foreground="#FF333333" BorderBrush="#FF959595" Margin="1,0" 
                      VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.ColumnSpan="3">
                <StackPanel x:Name="SpnOrdem"                                
                    Orientation="Horizontal"
                    VerticalAlignment="Center" Margin="0,-4,0,0">
                    <RadioButton x:Name="rbnCodigo"
                        Style="{StaticResource RadioButtonCadastro}"
                        Content="Código" Margin="5,0" Foreground="#FF333333" FontSize="13"
                        IsChecked="{Binding CodigoChecked}"/>
                    <RadioButton x:Name="rbnDescricao"
                        Style="{StaticResource RadioButtonCadastro}"
                        Content="Descrição" Margin="20,0,5,0" Foreground="#FF333333" FontSize="13"
                        IsChecked="{Binding DescricaoChecked}"/>
                </StackPanel>
            </GroupBox>

我的问题是:如何使单选按钮rbnCodigo已标记为 true?

如何启动单选按钮 在 WPF MVVM 应用程序中选中 = true

您是否尝试在 ViewModel 构造函数中将 CodigoChecked 初始化为 true?

Ed Plunkett 有正确的答案,只需将以下内容添加到您的构造函数中即可

CodigoChecked = true;

所以你的构造函数应该看起来像这样:

public SEL_PG_C_ALIViewModel(ICAD_EF_C_ALIService cadEfCAliService)
    {
        _cadEfCAliService = cadEfCAliService;
        IsDataLoaded = false;
        OnLoaded = new RelayCommand(OnLoadedExecute);
        Gerar = new RelayCommand(GerarExecute, GerarCanExecute);
        CodigoChecked = true;
    }