[英]Binding to a ListBox SelectedItem
I have a Window with ListBox on the left Side and TextBox on the right Side. 我有一个带有左侧列表框和右侧文本框的窗口。 The Textbox is binding to the Selected Item of the ListBox.
文本框绑定到列表框的选定项。 The Textbox has a SaveContentCommand.
文本框具有一个SaveContentCommand。
If you leave the Textbox with Enter or the Tab the SaveContentCommand is executed correct. 如果使用Enter或Tab离开文本框,则将正确执行SaveContentCommand。 But if i use the mouse to select something else the selecteditem is changed and then the SaveContentCommand is executed.
但是,如果我使用鼠标来选择其他内容,则更改selecteditem,然后执行SaveContentCommand。 This means the SaveContentCommand is used on another item.
这意味着SaveContentCommand用于另一个项目。
I have tried to hack something like RenameLastSelectedItem() But is there a correct/better way? 我曾尝试破解RenameLastSelectedItem()之类的东西,但是有没有正确/更好的方法?
My List: 我的列表:
<customControls:MyListBox x:Name="UserListBox"
Grid.Row="1"
Grid.Column="0"
ItemsSource="{Binding Users}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Top"
Style="{DynamicResource MyListBoxStyle}"
ItemContainerStyle="{DynamicResource MyListBoxItemUserListStyle}">
My TextBox: 我的文字框:
<customControls:MyTextBox x:Uid="textBoxName"
x:Name="textBoxNameOfSelectedItems"
Text="{Binding SelectedItem.Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true, Mode=TwoWay}"
focus:FocusExtension.IsFocused="{Binding SelectedItem.IsNameFocused, Mode=TwoWay}"
focus:FocusExtension.EnableSelection="True"
UseKeyboardBinding="true"
Style="{DynamicResource MyTextBoxStyle}"
SaveContentCommand="{Binding SelectedItem.UpdateCommand}"
In the set method of the SelectedItem, save the previous item. 在SelectedItem的set方法中,保存上一个项目。 This code will run when the selected item is changed when selecting a new item with the mouse..
当使用鼠标选择新项目时,如果更改了所选项目,则此代码将运行。
SelectedItem
{get { return _selectedItem;}
set
{
//null check
SaveContent(_selectedItem);
_selectedItem = value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.