列表框不跟踪用户输入
本文关键字:用户 输入 跟踪 列表 | 更新日期: 2023-09-27 18:10:38
我有一个显示TextBox
es动态数量的ListBox
。用户将在这些框中输入文本。当提交按钮被点击时,我需要能够访问用户输入的文本,应该在ListBox.Items
,像这样:
//Called on Submit button click
private void SaveAndSubmit(object sender, ExecutedRoutedEventArgs e)
{
var bounds = MyListBox.Items;
}
但是MyListBox.Items
在我最初设置ItemsSource
之后没有改变,这里:
//Field declaration
//Bounds is containing a group of strings that represent the boundaries
//for a contour plot. The min/max values are stored at the front and back
//of the group. However, there can be any number of dividers in between.
public ObservableCollection<string> Bounds { get; set; }
...
//Initialize Bounds in the constructor
//Called when the selected item for DVList (an unrelated ListBox) is changed
private void DVSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedDV = DVList.SelectedItem as DVWrapper;
if (selectedDV != null)
{
//Setting min/max
Bounds[0] = selectedDV.MinValue;
Bounds[Bounds.Count - 1] = selectedDV.MaxValue;
MyListBox.ItemsSource = Bounds;
}
}
我的XAML是这样的:
<Window.Resources>
<Style x:Key="BoundsStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
...
<TextBox/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Focusable" Value="False"/>
</Style>
</Window.Resources>
...
<ListBox Name="MyListBox"
ItemContainerStyle="{StaticResource BoundsStyle}"/>
因此,当SaveAndSubmit
被调用时,bounds
最终成为我最初在DVSelectionChanged
中设置的值。换句话说,列表框不会根据用户在列表框中包含的文本框中输入的内容进行更新。我怎样才能得到更新的ListBoxItem
?我想我的问题和这个类似,但是现在它不适合我。
当我在调试器中逐步执行时,我可以获得单个ListBoxItem
s。然而,它们的内容是空的。我正在调查这件事。
你需要绑定文本框的内容。
<TextBox/>
需要更改为<TextBox Content="{Binding}"/>
但是遵循MVVM,否则很难发现这些错误