1

嗨基本上我有一个使用 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已经标记为真的单选按钮?

4

2 回答 2

2

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

于 2013-09-17T20:06:40.630 回答
1

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;
    }
于 2013-09-17T22:18:54.853 回答