另一个小问题 . 我有一个包含对象的列表框 . 我有一些文本框,其中包含列表框中所选项目的一些属性,我必须编辑它们 . 但我的问题是:当我选择一个项目时,我用一些文本填充文本框 . 他保存了它,但是如果我点击另一个项目,我填写的东西留在文本框中,我该如何解决?
这是一个文本框的示例:所以我想设置我的对象的最大时间...当我点击列表框中的某些内容时,文本框变得可见 .
<TextBox Height="23" Visibility="{Binding Path=Visible, Converter={StaticResource boolToVis},UpdateSourceTrigger=PropertyChanged}" Text="{Binding Path=MaxTime,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" HorizontalAlignment="Left" Margin="376,322,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
这是我的列表框:
<ListBox SelectedItem="{Binding Path=SelectedQuestionDropList, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
DisplayMemberPath="Description"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True" Margin="0,201,0,204" Background="#CDC5CBC5"
dd:DragDrop.DropHandler="{Binding}" />
这是在我的viewmodel中:
public int MaxTime
{
get { return maxTime; }
set { maxTime = value;
OnPropertyChanged("MaxTime");
this.examQuestion.MaxTime = value;
}
}
2 回答
可能需要更改
TextBox
的Text
属性的UpdateSourceTrigger
并将其设置为LostFocus
而不是PropertyChanged
.参考:UpdateSourceTrigger on MSDN .
每次在列表框中选择一个项目时,都必须更新MaxTime属性 . 否则,在列表框中选择项目时,Textbox.Text值将不会更新 .
编辑:有很多方法可以做到这一点:)你的listbox selectedItem绑定到SelectedQuestionDropList . 所以只需添加
到了二传手 . 此解决方案导致您的MaxTime属性与SelectedQuestionDropList属于同一个类 . 另一种简单的方法是使用列表框中的SelectionChanged事件,并使用MaxTime属性调用类上的方法(例如NotifyAll()) .