0
晚報畢竟叫,我受夠了的SelectionChanged (TabControl的)的引發LostFocus (文本框)事件之前事件被調用遇到的問題。事件的優先級:引發LostFocus被SelectionChange
這是一個問題,因爲SelectionChanged在選項卡更改期間被觸發,該實習生將ListView.SelectedIndex (TabControl> TabItem> ListView)重置爲-1。
該文本框使用LostFocus來更新/驗證它的textbox.text,這取決於SelectedIndex。文本框中的文本是從List<string>
存儲/檢索的,並且由於索引更改,List恰好超出了界限。
我環顧四周,厭倦了一些事情也是一種「黑客」的做法,並沒有真正幫助。
<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;
}
}