WPF - 如何通过按下按钮来复制控件/整个网格?
我是WPF的新手,我试图制作一个控件,它会在每次打到特定控件时扩展我的表单,直到它达到最多7次。WPF - 如何通过按下按钮来复制控件/整个网格?
这是形式: 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事件处理程序代码做到这一点?
回答:
这是我的评论的插图。我尽量保持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());}
}
}
以上是 WPF - 如何通过按下按钮来复制控件/整个网格? 的全部内容, 来源链接: utcz.com/qa/258514.html