Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将文本输出到文本框?_C#_Wpf - Fatal编程技术网

C# 如何将文本输出到文本框?

C# 如何将文本输出到文本框?,c#,wpf,C#,Wpf,我正在制作一个回文WPF程序,测试单词是否是回文。 所以我希望用户键入他想要的任何内容 我在stackoverflow上看到有人有类似的问题,他使用了 OutputText.Text = (word + Environment.NewLine); 但这对我没有帮助 private void InputText_TextChanged(object sender, TextChangedEventArgs e) { while (true) { string s

我正在制作一个回文WPF程序,测试单词是否是回文。 所以我希望用户键入他想要的任何内容

我在stackoverflow上看到有人有类似的问题,他使用了

OutputText.Text = (word + Environment.NewLine);
但这对我没有帮助

private void InputText_TextChanged(object sender, TextChangedEventArgs e)
{
    while (true)
    {
        string s = Console.ReadLine();
        InputText.Text += s.ToString();
    }
}

private void OutputText_TextChanged(object sender, TextChangedEventArgs e)
{
    while (true)
    {
        foreach (var word in InputText.Text.ToString())
        {
            OutputText.Text = word.ToString();
        }
    }
}
我应该能够输入任何我想要的单词,如果它是回文的话,我应该能够输出它。我知道我需要一个方法,但现在我想要和输出完全相同的输入

例如

输入:我有雷达

输出:我有雷达

为什么无限循环而真{…}和控制台?大概是这样的:

//TODO: your routine which tests for palindrome
private static bool IsPalindrome(string value) 
{
    ...
}

private void InputText_TextChanged(object sender, TextChangedEventArgs e)
{
    // This event handler runs when user changes text in InputText
    // i.e. input anything into InputText 
    // We should:

    // 1. Obtain user input
    string word = InputText.Text;

    // 2. Check for being palindrome
    bool isPalindrome = IsPalindrome(word); 

    // 3. Output results into OutputText
    OutputText.Text = word + (isPalindrome ? " is a palindrome" : " is NOT a palindrome");
}
public class MyViewModel: INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propName) { 
        PropertyChanged?.Invoke(this, new OnPropertyChangedEventArgs(propName)); 
    }

    private string textBox1Text;

    public string MyFirstTextBox {
        get => textBox1Text;
        set {
            textBox1Text = value;
            OnPropertyChanged(nameof(MyFirstTextBox));
            MySecondTextBox = value; // Set value of second text box
        }
    }

    private string textBox2Text;

    public string MySecondTextBox {
        get => textBox2Text;
        set {
            textBox2Text = value;
            OnPropertyChanged(nameof(MySecondTextBox));
        }
    }

}

请注意,我们不希望任何OutputText被更改,如果OutputText被更改,我们该怎么办?

考虑到您正在使用WPF,您还应该考虑使用。 这是一个编程范例,它封装了代码背后的视图。基本上,您看到的视图不知道数据来自何处

这就是WPF的用途

假设您有两个文本框,其中一个文本框中的代码将填充另一个文本框

这是你的两个文本框

<TextBox Text="{Binding MyFirstTextBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{DynamicResource MyVM}" />
<TextBox Text="{Binding MySecondTextBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{DynamicResource MyVM}" />
现在您已经有了一个非常基本的MVVM实现,这将进一步帮助您

如果您对使用MVVM不感兴趣,那么可以继续使用事件

在您的代码隐藏中:

private void InputText_TextChanged(object sender, EventArgs e) => OutputText.Text = (sender as TextBox).Text;

private void OutputText_TextChanged(object sender, EventArgs e) => Debug.WriteLine("Whoo! My text changed to {0}", (sender as TextBox).Text);
编辑:将最后一段代码添加到代码标记中。
另外编辑:抱歉,如果我在代码中犯了错误,这是我午餐休息时脑子里想不出来的

为什么while为true?这样用户就可以始终键入文本,我在想,在用户键入文本时制作一个程序,该程序已经在检查它是否是回文了。为什么您要从控制台阅读?@Johnnymapp我是WPF的新手。直到一周前,我还只是使用控制台编程。Ty MVVM是我的下一步
private void InputText_TextChanged(object sender, EventArgs e) => OutputText.Text = (sender as TextBox).Text;

private void OutputText_TextChanged(object sender, EventArgs e) => Debug.WriteLine("Whoo! My text changed to {0}", (sender as TextBox).Text);