如何确定某项是否是 WPF ItemTemplate 中的最后一项?
我有一些
<ItemsControl Name="mItemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Mode=OneWay}" KeyUp="TextBox_KeyUp"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
绑定到简单 ObservableCollection 的
private ObservableCollection<string> mCollection = new ObservableCollection<string>();
public MainWindow()
{
InitializeComponent();
this.mCollection.Add("Test1");
this.mCollection.Add("Test2");
this.mItemsControl.ItemsSource = this.mCollection;
}
XAML在 last 文本框中按 Enter 键后,我希望出现另一个文本框。我有代码可以做到这一点,但有一个差距:
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
{
return;
}
TextBox textbox = (TextBox)sender;
if (IsTextBoxTheLastOneInTheTemplate(textbox))
{
this.mCollection.Add("A new textbox appears!");
}
}
函数 IsTextBoxTheLastOneInTheTemplate() 是我需要的东西,但不知道如何编写。我该如何去写呢?
我考虑过使用 ItemsControl.ItemContainerGenerator,但无法将所有部分放在一起。
谢谢!
-麦克风
I have some XAML
<ItemsControl Name="mItemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Mode=OneWay}" KeyUp="TextBox_KeyUp"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
that's bound to a simple ObservableCollection
private ObservableCollection<string> mCollection = new ObservableCollection<string>();
public MainWindow()
{
InitializeComponent();
this.mCollection.Add("Test1");
this.mCollection.Add("Test2");
this.mItemsControl.ItemsSource = this.mCollection;
}
Upon hitting the enter key in the last TextBox, I want another TextBox to appear. I have code that does it, but there's a gap:
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
{
return;
}
TextBox textbox = (TextBox)sender;
if (IsTextBoxTheLastOneInTheTemplate(textbox))
{
this.mCollection.Add("A new textbox appears!");
}
}
The function IsTextBoxTheLastOneInTheTemplate() is something that I need, but can't figure out how to write. How would I go about writing it?
I've considered using ItemsControl.ItemContainerGenerator, but can't put all the pieces together.
Thanks!
-Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设这是您正在处理的事情的简化版本。单向绑定到字符串集合的文本框对我来说没有意义。
本例中的主要问题是使用简单的字符串作为项目源。我假设我们不能保证字符串是唯一的,所以我们不能从 textbox.Text 得出任何结论。另外,由于字符串是不可变的,因此我们不能使用字符串的实例来推断任何内容。
解决方案的第一步是创建一个类来保存我们可以引用的数据。 (在这种情况下,这似乎有点愚蠢,因为它只包含一个字符串。)
您的第二个代码块变为:
我们将使用文本框的 Tag 属性来存储对绑定源的引用。我们将用它来解决唯一性问题。 XAML 变为:
最后,处理程序变为:
I'm assuming this is a simplified version of what you're working on. A textbox with oneway binding to a string collection doesn't make sense to me.
The main problem in this case is using a simple string as the item source. I'm assuming we can't guarantee the strings will be unique so we can't draw any conclusions from textbox.Text. Also, since strings are immutable, we can't use the instance of the string to infer anything.
The first step in the solution is to create a class to hold the data that we can refer to. (This seems a little silly in this case as it just holds a string.)
Your second code block becomes:
We'll use the Tag property of the textbox to store a reference to our binding source. We'll use this to get around the uniqueness issues. The XAML becomes:
Lastly, the handler becomes:
我能够通过参考 获得一个不错的解决方案http://drwpf.com/blog/2008/07/20/itemscontrol-g-is-for-generator/。不是超级优雅,但它对我有用。
I was able to get a decent solution by referring to http://drwpf.com/blog/2008/07/20/itemscontrol-g-is-for-generator/. Not super-elegant, but it worked for me.
在我看来,这是在视图模型中最好定义的行为:
从这里开始,当用户按下 ENTER 时,让
TextBox
更新其源代码是一个简单的事情:使用
KeyUp
代码> 事件处理程序:It seems to me that this is behavior best defined in the view model:
From here, it's a simple matter of making the
TextBox
update its source when the user presses ENTER:With the
KeyUp
event handler: