【发布时间】:2010-05-17 03:07:08
【问题描述】:
我有一个列表框,每个列表项中有一堆控件。
<ListBox x:Name="projectList" IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
<ListBox x:Name="taskList" ItemsSource="{Binding Tasks}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox x:Name="textBoxTask" />
<Button
x:Name="ButtonAddNewTask"
Content="Test"
CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}"
Click="ButtonAddNewTask_Click"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
当我单击列表框中的按钮时,我想向列表框中的列表框添加一个新项目。我已经走到这一步了。所以我的问题是如何获取文本框以及如何更新列表框?
这是我的点击事件
private void ButtonAddNewTask_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
Project proj = button.DataContext as Project;
if(proj.Tasks == null)
proj.Tasks = new List<Task>();
proj.Tasks.Add(new Task("Added Task"));
}
感谢
【问题讨论】:
-
“获取文本框”到底是什么意思?
-
目标是使用文本框中的文本创建任务。 TextBox newTaskTextBox = somthing here; proj.Tasks.Add(new Task(newTaskTextBox.Text));