我有一个标签控件...动态添加标签项。每个标签项都包含一个数据网格 我的标签控件是
<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"));
}
}
我将标签项添加为
TabItems.Add(new TabItem());
DataRow dr = GridSource.NewRow();
dr["Column1"] = "abc";
dr["Column2"] = "abc";
dr["Column3"] = "abc";
GridSource.Rows.Add(dr);
但我的用户界面没有更新。任何人都可以告诉我可能的原因吗?
答案 0 :(得分:1)
我不确定DataTable是否实现了更新UI所需的通知机制。从您的代码中,我可以建议一个简单的替代方法,即创建DataTable并向其添加数据,然后设置属性“GridSource”。
根据您现有的代码属性,在创建新的DataTable时会触发更改,并且此时将其设置为“GridSource”,DataTable为空并且相应地呈现UI。
<强>更新强>
我的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;
}
}
}