晚上,我遇到了在 LostFocus (TextBox)事件之前调用SelectionChanged (TabControl)事件的问题。
这是一个问题,因为在选项卡更改期间触发了 SelectionChanged,实习生将 ListView.SelectedIndex (TabControl>TabItem>ListView)重置为 -1。
文本框使用 LostFocus 更新/验证它的 textbox.text 取决于 SelectedIndex。文本框中的文本是从 a 存储/检索的,List<string>
并且由于索引已更改,因此 List 恰好超出范围。
我环顾四周,厌倦了一些东西,也是一种“hack-y”方法,但并没有真正帮助。
<TabControl SelectionChanged="SelectionChanged_Tab"
<TabItem .../>
<TabItem .../>
</TabControl>
<Grid>
<Label .../>
<TextBox Name="Name" LostFocus="Lost_Focus" .../>
</Grid>
代码:
private void SelectionChanged_Tab(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl)
{
ListView1.SelectedIndex = -1;
ListView2.SelectedIndex = -1;
}
}
private void Lost_Focus(object sender, RoutedEventArgs e)
{
TextBox textbox = sender as TextBox;
int Index = ListView.SelectedIndex;
if (string.IsNullOrEmpty(textbox.Text) || textbox.Text == "0")
{
textbox.Text = "0";
}
switch (textbox.Name)
{
case "Name":
SomeList[Index].AProperty = textbox.Text;
break;
}
}