我有一个ListBox
显示动态数量的TextBox
es。用户将在这些框中输入文本。单击提交按钮时,我需要能够访问用户输入的文本,应该是 at 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
最终将成为我最初将其设置为 in 的内容DVSelectionChanged
。换句话说,列表框不会根据用户输入到列表框中包含的文本框中的内容进行更新。如何获得更新ListBoxItem
的 s?我认为我的问题与此类似,但目前对我不起作用。
当我在调试器中单步执行时,我可以获得单独ListBoxItem
的 s. 但是,他们的内容是空的。我现在正在调查。