0

我有一个选项卡控件...动态添加到哪些选项卡项。每个选项卡项都包含一个数据网格我的选项卡控件是

 <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);

但我的用户界面没有更新..谁能告诉我可能的原因?

4

1 回答 1

1

我不确定 DataTable 是否实现了必要的通知机制来更新 UI。从您的代码中,我可以建议一个简单的替代方法,即创建一个 DataTable 并向其中添加数据,然后设置属性 "GridSource" 。

根据您现有的代码属性,当您创建新的 DataTable 并将其设置为“GridSource”时会触发更改,此时 DataTable 为空并相应地呈现 UI。

更新

我的xml代码:

<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;
        }
    }
}
于 2013-08-27T06:32:23.853 回答