Как установить значение на основе радиокнопки - PullRequest
0 голосов
/ 06 февраля 2020

Мой вопрос заключается в следующем. У меня есть вид, с двумя переключателями: Текущий и История, когда выбран Текущий, текстовое поле год по умолчанию соответствует текущему году:

enter image description here

При нажатии кнопки «История» вы можете ввести свой собственный год: enter image description here

Проблема, с которой я сталкиваюсь, заключается в том, что когда я go возвращаюсь к текущему состоянию, мне нужно текстовое поле для отображения текущего года снова. В настоящее время он содержит только то, что было в последний раз введено в текстовое поле, как показано здесь:

enter image description here

ViewModel :

int _year;
        public int Year
        {
            get
            {
                if (IsCurrentSelected)
                    return AppSession.Repository.Settings.CurrentAppraisalYear;
                else if (IsHistorySelected)
                    return _year;
                else
                    return AppSession.Repository.Settings.CurrentAppraisalYear;
            }
            set
            {
                _year = value;
                OnPropertyChanged("Year");
            }
        }

Просмотр (XAML)

<RadioButton x:Name="btnCurrent" IsChecked="{Binding IsCurrentSelected, UpdateSourceTrigger=PropertyChanged}" Content="Current" Grid.Column="0" Grid.Row="0"/>
<RadioButton x:Name="btnHistory" IsChecked="{Binding IsHistorySelected, UpdateSourceTrigger=PropertyChanged}" Content="History" Grid.Row="0" Grid.Column="1"/>

                <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="5,0" IsEnabled="{Binding IsHistorySelected, Converter={StaticResource EnabledConverter} }">
                    <TextBlock Text="Year:"  Width="100" VerticalAlignment="Center"/>
                    <tools:NumberTextBox x:Name="txtYear" FocusManager.FocusedElement="{Binding ElementName=txtYear}" Width="100"  Text="{Binding Path=Year, UpdateSourceTrigger=PropertyChanged}"/>
                </StackPanel>

1 Ответ

0 голосов
/ 07 февраля 2020
private bool _isCurrentSelected;
public bool IsCurrentSelected
{
     get => _isCurrentSelected;
     set
     {
         if(_isCurrentSelected == value)
             return;
         _isCurrentSelected = value;
         OnPropertyChanged("IsCurrentSelected");
         OnPropertyChanged("Year");
     }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...