2013-08-27 42 views
0

我有一個標籤控件......爲了這片項目被添加dynamically.Each標籤項包含一個數據網格 我的標籤conrtol是自定義的Tabcontrol標籤ItemDatabinding

<TabControl Grid.Row="1" Name="tabControl" ItemsSource="{Binding TabItems}" ContentTemplate="{DynamicResource DataTemplate1}" > 
      </TabControl> 

模板...

<Window.Resources> 
     <DataTemplate x:Key="DataTemplate1"> 
      <Grid> 
       <DataGrid ItemsSource="{Binding Path=GridSource,UpdateSourceTrigger=PropertyChanged}"></DataGrid> 
    </Grid> 
     </DataTemplate> 
    </Window.Resources> 




public MainWindowViewModel() 
     { 
      NewCmnd = new RelayCommand(NewCommandExecute, NewCommandCanExecute); 
      TabItems = new ObservableCollection<TabItem>(); 
      GridSource = new DataTable(); 
      GridSource.Columns.Add("Column1"); 
      GridSource.Columns.Add("Column2"); 
      GridSource.Columns.Add("Column3"); 
     } 

     public ObservableCollection<TabItem> TabItems 
     { 
      get; 
      set; 
     } 
     public DataTable GridSource 
     { 
      get 
      { 
       return dt; 
      } 
      set 
      { 
       dt = value; 
       OnPropertyChanged(new PropertyChangedEventArgs("GridSource")); 
      } 
     } 

而且IAM添加標籤的項目,如

TabItems.Add(new TabItem()); 
      DataRow dr = GridSource.NewRow(); 
      dr["Column1"] = "abc"; 
      dr["Column2"] = "abc"; 
      dr["Column3"] = "abc"; 
     GridSource.Rows.Add(dr); 

,但我的UI是不是updated..Can任何一個可以告訴米e可能的原因?

回答

1

我不確定DataTable是否實現了必要的通知機制來更新UI。從你的代碼中,我可以建議創建一個DataTable並向其中添加數據的簡單方法,然後設置屬性「GridSource」。

根據你現有的代碼屬性改變,當你創建一個新的DataTable並在此時將它設置爲「GridSource」時,會觸發DataTable爲空,並且相應地呈現UI。

UPDATE

我的XAML代碼:

<Window.Resources> 
    <DataTemplate x:Key="DataTemplate1"> 
     <Grid MinHeight="100"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="25"/> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <Button Content="Add Row" Click="Button_Click" Grid.Row="0"></Button> 
      <DataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="True" Grid.Row="1"/> 
     </Grid> 
    </DataTemplate> 
</Window.Resources> 
<Grid> 
    <StackPanel> 
     <Button Content="Add TabItem" Click="Button_Click_1"/> 
     <TabControl ItemsSource="{Binding TabItems}" ContentTemplate="{StaticResource DataTemplate1}" MinHeight="100"></TabControl> 

    </StackPanel> 
</Grid> 

這是我的代碼背後,雖然沒有跟隨MVVM

public partial class MainWindow : Window 
{ 
    private ObservableCollection<TabItemContainer> tabItems; 


    public ObservableCollection<TabItemContainer> TabItems 
    { 
     get { return tabItems; } 
     set 
     { 
      tabItems = value; 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     TabItems = new ObservableCollection<TabItemContainer>(); 
     TabItemContainer container = new TabItemContainer(); 
     TabItems.Add(container); 

     DataTable table = container.Data; 


     DataRow row = table.NewRow(); 
     row["Name"] = "ABC"; 
     row["Amount"] = 10; 
     table.Rows.Add(row); 
     container.Data = table; 
     this.DataContext = this; 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     TabItemContainer container = ((Button)sender).DataContext as TabItemContainer; 
     DataRow row = container.Data.NewRow(); 
     row["Name"] = "ABC"; 
     row["Amount"] = 10; 
     container.Data.Rows.Add(row); 
    } 

    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     TabItems.Add(new TabItemContainer()); 
    } 


} 

public class TabItemContainer 
{ 
    private DataTable data; 

    public DataTable Data 
    { 
     get 
     { 
      if (data == null) 
      { 
       data = new DataTable(); 
       data.Columns.Add("Name", typeof(string)); 
       data.Columns.Add("Amount", typeof(int)); 
      } 
      return data; 
     } 
     set 
     { 
      data = value; 
     } 
    } 
} 
+0

所以不會酒店在代碼行改變了火「GridSource.Rows.Add(dr);」 ??? – WpfBegnner

+0

除非DataTable.Row提供設施。我相信你的TabItems必須將它作爲它的ObsservableCollection。 – AsitK

+0

添加行後,您還可以嘗試「GridSource = GridSource」。 – AsitK