gpt4 book ai didi

wpf - 如何将 List 集合绑定(bind)到 WPF 中的 TabControl header ?

转载 作者:行者123 更新时间:2023-12-03 11:04:57 28 4
gpt4 key购买 nike

我可以将数据输入到我的 TabControl 中,但标题周围有框架,我不能从一个选项卡滑到另一个选项卡。

我在这个 TabControl 上的 XAML 绑定(bind)语法做错了什么?

XAML:

<StackPanel>
<TabControl x:Name="TheTabControl">
<TabControl.ItemTemplate>
<DataTemplate>
<TabItem Header="{Binding LastName}">
<StackPanel Margin="10" Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</TabItem>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>

<TabControl>
<TabItem Header="Tab1">
<TextBlock Text="This is a test of tab 1"/>
</TabItem>
<TabItem Header="Tab2">
<TextBlock Text="This is a test of tab 2"/>
</TabItem>
</TabControl>

</StackPanel>

后面的代码:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

//create all
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 23 });
customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 23 });

//show
TheListBox.ItemsSource = customers;

}
}

public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int NumberOfContracts { get; set; }
}

最佳答案

这是我会做什么

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
//create all
var customers = new List<Customer>{
new Customer {FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23},
new Customer {FirstName = "Jane", LastName = "Smith", NumberOfContracts = 23},
new Customer {FirstName = "John", LastName = "Tester", NumberOfContracts = 23}};

//show
TheTabControl.ItemsSource = customers;
TheTabControl.SelectedIndex = 0;
}


public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int NumberOfContracts { get; set; }
}

在 XAML 方面
<TabControl x:Name="TheTabControl">            
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding LastName}"/>
</TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock>
This is <TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding LastName}"/>
</TextBlock>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>

关于wpf - 如何将 List 集合绑定(bind)到 WPF 中的 TabControl header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/589802/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com