我是WPF的新手,我正在尝试制作一个控件,每次点击它时都会使用特定控件扩展我的表单,直到它达到最多7次。
这是表格: https://i.imgur.com/50tzCSy.png
<Border Padding="10">
<StackPanel>
<TextBlock Text="Insert a file or expand to insert more file at once." Margin="0 10"/>
<!-- Add File -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="File Name:" FontWeight="Bold" Margin="0 10" />
<TextBox Grid.Column="1" Height="20" Margin="10 0 0 0" />
<Button Grid.Column="2" Content="Test" Height="20" Width="50" />
</Grid>
<ItemsControl ItemsSource="{Binding AllChildren}" ItemTemplate="{StaticResource ChildTemplate}" />
<!-- Buttons -->
<Grid Margin="0 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Margin="0 0 10 0" Content="Save" />
<Button Grid.Column="1" Content="Expand" Grid.ColumnSpan="2" Margin="0.2,0,123.6,0.2" Click="Button_Click" />
<Button Grid.Column="2" Margin="10 0 0 0" Content="Cancel" />
</Grid>
</StackPanel>
</Border>
我尝试复制网格,直到最多重复7次。
如何通过Button Event处理程序代码实现此目的?
答案 0 :(得分:1)
以下是我的评论。我尝试将ViewModel保持为尽可能简单和通用,因此它看起来应该是空的。由你来修改它,并最终放一些DataBinding(我会推荐它)。
如果您需要更具体的内容,请告诉我们。 XAML:
<Border Padding="10">
<StackPanel>
<TextBlock Text="Insert a file or expand to insert more file at once." Margin="0 10"/>
<ListBox Name="DynamicList" HorizontalContentAlignment="Stretch" >
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:FileM}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="File Name:" FontWeight="Bold" Margin="0 10" />
<TextBox Grid.Column="1" Height="20" Margin="10 0 0 0" />
<Button Grid.Column="2" Content="Test" Height="20" Width="50" />
</Grid>
</DataTemplate>
</ListBox.Resources>
</ListBox>
<!--Since I don'T have any information about it, I removed the ItemsControl part-->
<Grid Margin="0 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Margin="0 0 10 0" Content="Save" />
<Button Grid.Column="1" Content="Expand" Click="Button_Click" />
<Button Grid.Column="2" Margin="10 0 0 0" Content="Cancel" />
</Grid>
</StackPanel>
</Border>
背后的代码:
public class FileM
{
}
public partial class MainWindow : Window
{
public ObservableCollection<FileM> ListFiles = new ObservableCollection<FileM>();
public MainWindow()
{
InitializeComponent();
DynamicList.ItemsSource = ListFiles;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if(ListFiles.Count<7){ListFiles.Add(new FileM());}
}
}