WPF迭代 [英] WPF Iteration
问题描述
你好,
使用.net 4& VS C#Express 2010,这对我来说都是新手,我正在完成一些基本的编程任务.
我正在尝试在WFP文本框中分配一个while循环计数,这很容易在wpf控制台中进行.
这是我的代码和xaml:
Hello,
Using .net 4 & VS C# Express 2010, new to both and I''m working through some basic programming tasks.
I''m trying to have a while loop count disaplyed in my WFP textbox which is easy enough to do in a wpf console.
Here''s my code and xaml:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfWhileLoop
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
int n = 1;
while (n < 6)
{
textBox1.Text = "current value of n is " + n++;
}
}
}
}
<Window x:Class="WpfWhileLoop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Height="311" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="503" AcceptsReturn="True" />
</Grid>
</Window>
当我运行它时,我得到:
n的当前值为5
我正在尝试显示每个增量,例如:
n的当前值为1
n的当前值为2
n的当前值为3
n的当前值为4
n的当前值为5
有什么想法可以使我工作吗?
When I run this I get:
current value of n is 5
I ''m trying to have each increment displayed, e.g.:
current value of n is 1
current value of n is 2
current value of n is 3
current value of n is 4
current value of n is 5
Any ideas how i can make this work?
Thanks!
推荐答案
最简单的方法如下:
The simplest way to do it is probably as follows:
textBox1.Text += "current value of n is " + (n++) + Environment.NewLine;
这会将字符串追加到Text属性的当前值的新值.
我假设这是一个简化的示例.如果您的实际要求更复杂,则可能需要查看将值列表绑定到类似ListBox
的东西,并使用数据模板对其进行适当的格式化.这将是更常规的"WPF"处理方式.
您可能需要考虑翻阅C#入门书籍.这是一个相当基本的字符串操作任务,您可能会从基础知识中受益.
This appends the string to new value to the current value of the Text property.
I''m assuming this is a simplified example. If you''re actual requirement is more complex, you may want to look at binding the list of values to something like a ListBox
and using data templates to format it appropriately. This would be a more normal ''WPF'' way of doing things.
You may want to consider looking through a C# for beginners book. This is a fairly basic string manipulation task, and you may benefit from working through the basics.
您只需要在每次迭代中添加新行.
Hi,
You just need to add a new line in each iteration.
while (n < 6)
{
textBox1.Text = textBox1.Text + "current value of n is " + n++ + Environment.NewLine;
}
瓦莱里.
Valery.
这篇关于WPF迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!