我需要添加包含Grid的Tabs。网格包含带有alredy定义样式的TextBoxes和Labels。如何以编程方式生成XAML代码(具有已存在元素的选项卡)?我可以这样做,还是需要创建每个元素,设置样式并添加到TabItem?这是代码的一部分:
<TabItem Header="tabItem1" Name="tabItem1">
<Grid Name="grid1" HorizontalAlignment="Stretch" VerticalAlignment="Top" DataContext="{Binding ElementName=tabControl1, Path=ActualWidth}" MinWidth="768" MinHeight="446">
<Grid.RowDefinitions>
<RowDefinition MinHeight="43" Height="*" />
<RowDefinition Height="*" MinHeight="45" />
<RowDefinition Height="*" MinHeight="435" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" MinWidth="100" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="1" Height="27" Name="textBox1" VerticalAlignment="Top" Margin="11,6,0,0" HorizontalAlignment="Stretch" Width="Auto" FontSize="14" HorizontalContentAlignment="Stretch" MinWidth="141" FlowDirection="LeftToRight" />
<Label Content="Supplier" Height="27" Name="label2" VerticalAlignment="Top" FontSize="14" FontFamily="Tahoma" FontWeight="Bold" Margin="21,6,0,0" Width="Auto" IsEnabled="True" HorizontalAlignment="Stretch" Foreground="Black" Background="White" MinWidth="133" HorizontalContentAlignment="Stretch" />
<TextBox Grid.Column="1" Grid.Row="1" FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="11,6,0,0" Name="textBox11" VerticalAlignment="Top" Width="Auto" MinWidth="141" />
<Label Grid.Row="1" Content="Supplier Bank" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label3" VerticalAlignment="Top" Width="Auto" Background="White" MinWidth="133" />
<TextBox Grid.Column="1" Grid.Row="2" FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="11,6,0,0" Name="textBox12" VerticalAlignment="Top" Width="Auto" MinWidth="141" />
<Label Grid.Row="2" Content="Account Number" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label4" VerticalAlignment="Top" Background="White" MinWidth="133" />
<TextBox Grid.Column="4" FontSize="14" Height="27" Margin="11,6,20,0" Name="textBox2" VerticalAlignment="Top" HorizontalAlignment="Stretch" Width="Auto" DataContext="{Binding ElementName=grid1, Path=ActualWidth}" MinWidth="141" />
<Label Grid.Column="3" Content="Buyer" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label5" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Background="White" MinWidth="133" />
<TextBox Grid.Column="4" Grid.Row="1" FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="11,6,20,0" Name="textBox3" VerticalAlignment="Top" Width="Auto" MinWidth="141" />
<Label Grid.Column="3" Grid.Row="1" Content="Buyer Bank" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label6" VerticalAlignment="Top" Width="Auto" HorizontalAlignment="Stretch" Background="White" MinWidth="133" />
<TextBox Grid.Column="4" Grid.Row="2" FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="11,6,20,0" Name="textBox4" VerticalAlignment="Top" Width="Auto" MinWidth="141" />
<Label Grid.Column="3" Grid.Row="2" Content="Account Number" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label7" VerticalAlignment="Top" Width="Auto" HorizontalAlignment="Stretch" Background="White" MinWidth="133" />
</Grid>
</TabItem>
答案 0 :(得分:2)
将TabControl.ItemsSource
属性绑定到ObservableCollection<SomeViewModel>
,然后使用DataTemplates定义每个ViewModel的外观。
this answer中的更多详情。
答案 1 :(得分:1)
您是否需要以编程方式执行此操作?
最好拥有一个可以包含每个TabItem
信息的类的集合,将TabControl
绑定到该集合并设置ItemTemplate
所以每个项目以同样的方式显示。
例如,您可以拥有这样的类:
public class BankMovement
{
public string Supplier { get; set; }
public string SupplierBank { get; set; }
// ... etc.
}
在您的viewmodel或代码隐藏中,创建该类型的集合。
public ObservableCollection<BankMovement> Movements { get; set; }
Movements = new ObservableCollection<BankMovement>();
Movements.Add(new BankMovement());
// add as many movements as you want
//tabControl1.ItemsSource = Movements; You can do this through Binding in the XAML, preferably
最后,在您的XAML中,将TabControl的ItemsSource
绑定到该集合,然后设置ItemTemplate
。此外,在DataTemplate
中,将TextBoxes绑定到BankMovement类的相应属性:
<TabControl ItemsSource="{Binding Movements}">
<TabControl.ItemTemplate>
<DataTemplate>
<Grid Name="grid1" HorizontalAlignment="Stretch" VerticalAlignment="Top" MinWidth="768" MinHeight="446">
<Grid.RowDefinitions>
<RowDefinition MinHeight="43" Height="*" />
<RowDefinition Height="*" MinHeight="45" />
<RowDefinition Height="*" MinHeight="435" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" MinWidth="100" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Text="{Binding Supplier}" Grid.Column="1" Height="27" Name="textBox1" VerticalAlignment="Top" Margin="11,6,0,0" HorizontalAlignment="Stretch" Width="Auto" FontSize="14" HorizontalContentAlignment="Stretch" MinWidth="141" FlowDirection="LeftToRight" />
<Label Content="Supplier" Height="27" Name="label2" VerticalAlignment="Top" FontSize="14" FontFamily="Tahoma" FontWeight="Bold" Margin="21,6,0,0" Width="Auto" IsEnabled="True" HorizontalAlignment="Stretch" Foreground="Black" Background="White" MinWidth="133" HorizontalContentAlignment="Stretch" />
<TextBox Text="{Binding SupplierBank}" Grid.Column="1" Grid.Row="1" FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="11,6,0,0" Name="textBox11" VerticalAlignment="Top" Width="Auto" MinWidth="141" />
<Label Grid.Row="1" Content="Supplier Bank" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label3" VerticalAlignment="Top" Width="Auto" Background="White" MinWidth="133" />
<!-- etc. -->
</Grid>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>