2 回答

TA貢獻1752條經(jīng)驗 獲得超4個贊
是DataContext
當(dāng)前作用域中綁定的上下文。是TabControl
一個ItemsControl
,它有一個ItemsSource
需要一個IEnumerable
(IEnumerable<Tab>
在本例中)。您應(yīng)該引入一個視圖模型,它充當(dāng) the?DataContext
,在本例中公開the綁定到的UserControl
源集合。視圖模型通常將托管視圖可以綁定到的所有數(shù)據(jù)。視圖模型通常實現(xiàn)接口,以便 UI 控件在綁定源更改時自動更新。ObservableCollection<Tab>
TabControl
INotifyPropertyChanged
Tab.cs(選項卡控件將綁定到的數(shù)據(jù)模型):
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條經(jīng)驗 獲得超5個贊
根本問題是,這些為多個項目生成內(nèi)容的控件應(yīng)該使用 屬性(而ItemsSource
不是DataContext
.
該行 TabControl.DataContext = clsTabs;
應(yīng)分配給TabControl.ItemsSource
此時您將在輸出窗格中看到
System.Windows.Data 錯誤:40:BindingExpression 路徑錯誤:在“對象”“選項卡”(HashCode=55467050) 上找不到“上下文”屬性。BindingExpression:路徑=上下文;DataItem='Tab'(哈希碼=55467050);目標(biāo)元素是“DataGrid”(名稱=“”);目標(biāo)屬性是“DataContext”(類型“Object”)
該行 <DataGrid AutoGenerateColumns="True" DataContext="{Binding Context}" />
存在先前的問題和錯誤的屬性名稱。應(yīng)該是 <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Content}" />
這樣,然后你就應(yīng)該很好。
- 2 回答
- 0 關(guān)注
- 480 瀏覽
添加回答
舉報