2 回答
TA贡献1752条经验 获得超4个赞
是DataContext
当前作用域中绑定的上下文。是TabControl
一个ItemsControl
,它有一个ItemsSource
需要一个IEnumerable
(IEnumerable<Tab>
在本例中)。您应该引入一个视图模型,它充当 the DataContext
,在本例中公开the绑定到的UserControl
源集合。视图模型通常将托管视图可以绑定到的所有数据。视图模型通常实现接口,以便 UI 控件在绑定源更改时自动更新。ObservableCollection<Tab>
TabControl
INotifyPropertyChanged
Tab.cs(选项卡控件将绑定到的数据模型):
public class Tab
{
public string TabName { get; set; }
public DataTable Content { get; set; }
public Tab(string name, DataTable content)
{
TabName = name;
Content = content;
}
public Tab(string name, List<string[]> content)
{
Content = new DataTable();
foreach (var item in content){
Content.Columns.Add(item[0], typeof(string));
}
DataRow row = Content.NewRow();
foreach (var item in content)
{
row[item[0]] = item[1];
}
Content.Rows.Add(row);
TabName = name;
}
}
ViewModel.cs(DataContext将集合UserControl公开Tab为绑定上下文的):
class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
this.ClsTabs = new ObservableCollection<Tab>();
ClsTabs.Add(new Tab("Animals", new List<string[]>() { new string[] { "Name", "Tiger" }, new string[] { "Tail", "Yes" } }));
ClsTabs.Add(new Tab("Vegetables", new List<string[]>() { new string[] { "Name", "Tomato" }, new string[] { "Color", "Red" }, new string[] { "Taste", "Good" } }));
ClsTabs.Add(new Tab("Cars", new List<string[]>() { new string[] { "Name", "Tesla" } }));
}
private ObservableCollection<Tab> clsTabs;
public ObservableCollection<Tab> ClsTabs
{
get => this.clsTabs;
set
{
if (Equals(value, this.clsTabs)) return;
this.clsTabs = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
测试.xaml.cs:
public partial class Test: UserControl
{
public Test()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
(this.DataContext as ViewModel)?.ClsTabs.Add(new Tab("New", new List<string[]>() { new string[] { "Name", "Something" }, new string[] { "Detail", "No" } }));
}
}
测试.xaml
<UserControl x:Class="WpfTestRange.Main.Test">
<!-- Set the DataContext of the Test control to an instance of ViewModel -->
<UserControl.DataContext>
<local:ViewModel />
</UserControl.DataContext>
<Grid>
<StackPanel>
<Button x:Name="Button"
Content="Add tab"
Click="Button_Click" />
<MetroAnimatedTabControl x:Name="TabControl"
ItemsSource="{Binding ClsTabs}"
TabStripPlacement="Left"
DisplayMemberPath="TabName">
<TabControl.ContentTemplate>
<DataTemplate DataType="local:Tab">
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Content}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</StackPanel>
</Grid>
</UserControl>
TA贡献1876条经验 获得超5个赞
根本问题是,这些为多个项目生成内容的控件应该使用 属性(而ItemsSource
不是DataContext
.
该行 TabControl.DataContext = clsTabs;
应分配给TabControl.ItemsSource
此时您将在输出窗格中看到
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“选项卡”(HashCode=55467050) 上找不到“上下文”属性。BindingExpression:路径=上下文;DataItem='Tab'(哈希码=55467050);目标元素是“DataGrid”(名称=“”);目标属性是“DataContext”(类型“Object”)
该行 <DataGrid AutoGenerateColumns="True" DataContext="{Binding Context}" />
存在先前的问题和错误的属性名称。应该是 <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Content}" />
这样,然后你就应该很好。
- 2 回答
- 0 关注
- 354 浏览
添加回答
举报