开发者

Update textboxes to selected item listbox

another little question. I have a listbox with objects. THen I have some textboxes with some properties of the selected item in the listbox and i have to edit them.. But my question is: when i select an item, and i fill the textboxes with some text. He saves it, but then if i click on another item, the things that i filled in stay in the textboxes, how do i solve this??

this is an example of a textbox: so i want to set maxtime of my object..the textbox becomes visible when i click on something in the listbox.

<TextBox Height="23" Visibility="{开发者_StackOverflow社区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" />

this is my listbox:

<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}"  />

this is in my viewmodel:

public int MaxTime
        {
            get { return maxTime; }
            set { maxTime = value;
            OnPropertyChanged("MaxTime");
            this.examQuestion.MaxTime = value;

            }
        }


Probably, you need to change the UpdateSourceTrigger for Text property of your TextBox and set it to LostFocus instead of PropertyChanged.

<TextBox Height="23" Visibility="{Binding Path=Visible, Converter={StaticResource boolToVis},UpdateSourceTrigger=PropertyChanged}" Text="{Binding Path=MaxTime,UpdateSourceTrigger=LostFocus,Mode=TwoWay}" HorizontalAlignment="Left" Margin="376,322,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />

Refer: UpdateSourceTrigger on MSDN.


you have to Update your MaxTime Property every time you select an item in your listbox. otherwise the Textbox.Text Value will not get updated while selecting items in your listbox.

EDIT: there are many ways to do that :) Your listbox selectedItem is bound to SelectedQuestionDropList. so just add

OnPropertyChanged("MaxTime"); 

to the setter. this solution cause that your MaxTime Property is within the same class as your SelectedQuestionDropList is. another simple way would be to use SelectionChanged event from your listbox, and the call a method (eg. NotifyAll()) on your class with MaxTime property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜