WPF dynamically add rows of TextBoxes to Grid layout
I have a window in WPF which contains a Grid
. The Grid
initially has one row and a TextBox
in that row. When the user clicks a Button
, I have to add a row to the grid with another TextBox
. While this seems doable, I need to the grid to be scrollable when the rows exceed the height of the grid. (this sort of resembles how you add attach开发者_如何学Pythonments to email. You add one, and then say add one more..and the list goes on). Am I going about this the right way or is there a better way to do this?
Can't answer whether you're going about it the right way as you've not supplied any code.
Here's how I'd do it. My view models:
public class AttachmentInfo : ViewModel
{
public string Path { get/set omitted }
}
public class EmailInfo : ViewModel
{
public ICollection<AttachmentInfo> Attachments { get omitted }
public ICommand AddAttachmentCommand { get omitted }
// logic for adding attachment simply adds another item to Attachments collection
}
In my view, something like this:
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Attachments}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<Button Command="{Binding AddAttachmentCommand}">Add</Button>
精彩评论