我有一些XAML
<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;
}
点击 last TextBox中的回车键后,我想要显示另一个TextBox。我有代码可以做到,但是有一个差距:
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,但不能将所有部分组合在一起。
谢谢!
-Mike
答案 0 :(得分:0)
我假设这是您正在处理的简化版本。单向绑定到字符串集合的文本框对我来说没有意义。
这种情况下的主要问题是使用简单的字符串作为项目源。我假设我们不能保证字符串是唯一的,所以我们不能从textbox.Text中得出任何结论。此外,由于字符串是不可变的,我们不能使用字符串的实例来推断任何内容。
解决方案的第一步是创建一个类来保存我们可以引用的数据。 (在这种情况下,这似乎有点傻,因为它只是一个字符串。)
class MyData
{
public string Value { get; set; }
}
您的第二个代码块变为:
ObservableCollection<MyData> mCollection = new ObservableCollection<MyData>();
public MainWindow()
{
InitializeComponent();
this.mCollection.Add(new MyData { Value = "Test1" });
this.mCollection.Add(new MyData { Value = "Test2" });
this.mItemsControl.ItemsSource = this.mCollection;
}
我们将使用文本框的Tag属性来存储对绑定源的引用。我们将用它来解决这些独特性问题。 XAML成为:
<ItemsControl Name="mItemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Value}" Tag="{Binding}" KeyUp="TextBox_KeyUp"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
最后,处理程序变为:
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
{
return;
}
TextBox textbox = (TextBox)sender;
if (mItemsControl.Items.IndexOf(textbox.Tag) == mItemsControl.Items.Count - 1)
{
this.mCollection.Add(new MyData() { Value = "A new textbox appears!" });
}
}
答案 1 :(得分:0)
通过引用http://drwpf.com/blog/2008/07/20/itemscontrol-g-is-for-generator/,我得到了一个不错的解决方案。不是超级优雅,但它对我有用。
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
{
return;
}
TextBox textbox = (TextBox)sender;
var lastContainer = this.mItemsControl.ItemContainerGenerator.ContainerFromIndex(this.mItemsControl.Items.Count - 1);
var visualContainer = (Visual)lastContainer;
var containedTextbox = (TextBox)GetDescendantByType(visualContainer, typeof(TextBox));
var isSame = textbox == containedTextbox;
if (isSame)
{
this.mCollection.Add("A new textbox appears!");
}
}
public static Visual GetDescendantByType(Visual element, Type type)
{
if (element.GetType() == type) return element;
Visual foundElement = null;
if (element is FrameworkElement)
(element as FrameworkElement).ApplyTemplate();
for (int i = 0;
i < VisualTreeHelper.GetChildrenCount(element); i++)
{
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = GetDescendantByType(visual, type);
if (foundElement != null)
break;
}
return foundElement;
}
答案 2 :(得分:0)
在我看来,这是在视图模型中最佳定义的行为:
public class ItemCollection : ObservableCollection<Item>
{
public ItemCollection()
{
// this guarantees that any instance created always has at least one
// item in it - you don't need this if you're creating instances in
// code, but if you just create them in XAML you do.
Item item = new Item(this);
Add(item);
}
}
public class Item
{
internal Item(ItemCollection owner)
{
Owner = owner;
}
public bool IsLast
{
get
{
return Owner.LastOrDefault() == this;
}
}
private ItemCollection Owner { get; set; }
private string _Value;
// here's the actual behavior: if the last item in the collection is
// given a non-empty Value, a new item gets added after it.
public string Value
{
get { return _Value; }
set
{
_Value = value;
if (IsLast && !String.IsNullOrEmpty(_Value))
{
Owner.Add(new Item(Owner));
}
}
}
}
从这里开始,当用户按下ENTER时,TextBox
更新其来源是一件简单的事情:
<DataTemplate DataType="{x:Type local:Item}">
<TextBox Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
KeyUp="TextBox_KeyUp"/>
</DataTemplate>
使用KeyUp
事件处理程序:
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
{
return;
}
TextBox t = (TextBox)sender;
BindingExpression be = t.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
}