简体   繁体   中英

WPF Click Button and Update Textblock to a new question

Im building a Customer Service style application, I have a list of questions that we want answering with a simple "Good" or "Bad" button response, Upon the response i want the Text block to move onto the next question. I have tried

private void GetQuestions()
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("https://***-***/api/v1/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var response = client.GetAsync("EPOS/GetQuestions").Result;

            if (response.IsSuccessStatusCode)
            {
                var questionList = response.Content.ReadAsAsync<IEnumerable<Questions>>().Result;

                foreach (var question in questionList)
                {
                    QuestionsBox.FontFamily = new FontFamily("Consolas");
                    QuestionsBox.SetValue(TextElement.FontSizeProperty, 20.0);
                    QuestionsBox.Text = question.QuestionText;
                }
            }
            else
            {
                MessageBox.Show("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase);
            }

        }

I cant figure out how to change the question to the next in the list from a button response.

I think I have an idea of what you're wanting from the question and it is that the answer being submitted then prompts the question to move to the next in the list.

MainWindowView:

<Window x:Class="TestWpfApplication.MainWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="450" 
        Width="800">

    <Window.Resources>
        <FontFamily x:Key="DefaultFont">Corbel</FontFamily>

        <Style TargetType="Button">
            <Setter Property="FontFamily" Value="{StaticResource DefaultFont}" />
            <Setter Property="FontSize" Value="50" />
        </Style>

        <Style TargetType="Label">
            <Setter Property="FontFamily" Value="{StaticResource DefaultFont}" />
            <Setter Property="FontSize" Value="20" />
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Label Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" Content="{Binding Path=QuestionText}" />

        <Grid Grid.Column="1" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="0" Command="{Binding Path=YesCommand}" Content="Yes" />
            <Button Grid.Column="1" Command="{Binding Path=NoCommand}" Content="No" />
        </Grid>
    </Grid>
</Window>

MainWindowViewModel:

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using TestWpfApplication.Annotations;

namespace TestWpfApplication
{
    internal class MainWindowViewModel : INotifyPropertyChanged
    {
        private RelayCommand _yesCommand;
        private RelayCommand _noCommand;
        private int _questionIndex;
        private string _questionText;
        private List<string> _questions;

        public MainWindowViewModel()
        {
            _questions = new List<string>
            {
                "Name your favourite football team",
                "Do you like F1",
                "Mothers Maiden Name",
                "Random Question"
            };

            QuestionText = _questions[_questionIndex];

            _yesCommand = new RelayCommand(o => Yes());
            _noCommand = new RelayCommand(o => No());
        }

        public string QuestionText
        {
            get => _questionText;
            set
            {
                _questionText = value;
                OnPropertyChanged(nameof(QuestionText));
            }
        }

        public ICommand YesCommand => _yesCommand;

        public ICommand NoCommand => _noCommand;

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void Yes()
        {
            // Logic for storing the answer to the question
            MoveToNextQuestion();
        }

        private void No()
        {
            // Logic for storing the answer to the question
            MoveToNextQuestion();
        }

        private void MoveToNextQuestion()
        {
            if (_questionIndex < _questions.Count - 1)
            {
                QuestionText = _questions[++_questionIndex];
            }
        }
    }
}

How you obtain the list of questions is completely up to you and also how you store the answers will be your own implementation but from this simple solution you can see how you can cycle through the list of questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM