none
Попытка вставить DataGrid в Tab C# WPF RRS feed

  • Общие обсуждения

  • Добрый вечер,
    имеется DataGrid
    связанный с моделью и Tab control
    не получается вставить DataGrid в Tab Control

    Большая просьба помочь.

    Закоментированный код, это попытки вставить Data DataGrid в Tab Control

     

    код Xaml:

    <Window x:Class="LibraryAppView.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" SizeToContent="Width">
    
    
        <DockPanel>
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="_File">
                    <MenuItem Click="MenuItem_Click" Header="E_xit" />
                </MenuItem>
            </Menu>
    
    
    
    
            <!--<DockPanel>
                            <Button Content="X" DockPanel.Dock="Right"></Button>
                        </DockPanel>-->
            <Grid>
    
                <Grid.RowDefinitions>
                    <RowDefinition />
                </Grid.RowDefinitions>
    
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="6*" />
                </Grid.ColumnDefinitions>
    
                <TabControl Grid.Column="1" Name="tabControl" ItemsSource="{Binding}">
                    <TabControl.Resources>
                        <DataTemplate x:Key="TabHeader" DataType="TabItem">
                            <DockPanel LastChildFill="True">
                                <Button  Name="btnDelete" Width="14" Height="19" DockPanel.Dock="Right" Content="X" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="5,0,0,0" Padding="0" Click="btnDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Tag}" />
                                <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=TabItem }, Path=Header}" />
                            </DockPanel>
                        </DataTemplate>
                        
                        <DataTemplate x:Key="TabBody" DataType="TabItem">
    
                <DataGrid Grid.Column="1"  Name="dataGridAllItems"  IsReadOnly="True" AlternatingRowBackground="Gainsboro" AutoGenerateColumns="False" AlternationCount="2" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Name" Binding="{Binding Path=ItemName}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Author" Binding="{Binding Path=ItemAuthor}"></DataGridTextColumn>
                        <DataGridTextColumn Header="ISBN" Binding="{Binding Path=ItemISBN}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Release Year" Binding="{Binding Path=ItemReleaseYear}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Category" Binding="{Binding Path=ItemCategory}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Genre" Binding="{Binding Path=ItemGenre}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Type" Binding="{Binding Path=ItemType}"></DataGridTextColumn>
                        <DataGridCheckBoxColumn Header="In Stock" Binding="{Binding Path=ItemInStock}" />
                    </DataGrid.Columns>
                </DataGrid>
    
                         
    
                        </DataTemplate>
                        
                        
                        
                        
                        
                        
                    </TabControl.Resources>
                    
                    <!--<TabItem>
                        <DataGrid Grid.Column="1"  Name="dataGridAllItems"  IsReadOnly="True" AlternatingRowBackground="Gainsboro" AutoGenerateColumns="False" AlternationCount="2" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Name" Binding="{Binding Path=ItemName}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Author" Binding="{Binding Path=ItemAuthor}"></DataGridTextColumn>
                        <DataGridTextColumn Header="ISBN" Binding="{Binding Path=ItemISBN}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Release Year" Binding="{Binding Path=ItemReleaseYear}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Category" Binding="{Binding Path=ItemCategory}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Genre" Binding="{Binding Path=ItemGenre}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Type" Binding="{Binding Path=ItemType}"></DataGridTextColumn>
                        <DataGridCheckBoxColumn Header="In Stock" Binding="{Binding Path=ItemInStock}" />
                    </DataGrid.Columns>
                </DataGrid>
                    </TabItem>-->
                    
                </TabControl>
    
                <StackPanel Grid.Column="0">
                    <Label  Name="lblSearch" Content="Search" Margin="0, 50, 0, 0" Background="AliceBlue" FlowDirection="LeftToRight" HorizontalContentAlignment="Center" BorderBrush="Black" BorderThickness="2" MouseLeftButtonUp="lblSearch_MouseLeftButtonUp"></Label>
                </StackPanel>
    
                <!--<TabItem Header="Items" Name="tabItemItems">
    <DataGrid Name="dataGridAllItems" AutoGenerateColumns="True" />
                
                </DataGrid>
    
                </TabItem>-->
    
    
    
                <!--<DataGrid Grid.Column="1"  Name="dataGridAllItems"  IsReadOnly="True" AlternatingRowBackground="Gainsboro" AutoGenerateColumns="False" AlternationCount="2" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Name" Binding="{Binding Path=ItemName}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Author" Binding="{Binding Path=ItemAuthor}"></DataGridTextColumn>
                        <DataGridTextColumn Header="ISBN" Binding="{Binding Path=ItemISBN}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Release Year" Binding="{Binding Path=ItemReleaseYear}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Category" Binding="{Binding Path=ItemCategory}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Genre" Binding="{Binding Path=ItemGenre}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Type" Binding="{Binding Path=ItemType}"></DataGridTextColumn>
                        <DataGridCheckBoxColumn Header="In Stock" Binding="{Binding Path=ItemInStock}" />
                    </DataGrid.Columns>
                </DataGrid>-->
    
    
            </Grid>
        </DockPanel>
    
    
        
        
        
        
        
        
        
    
    </Window>

    Также имеется функция для вставки в новые "табы" контента

    code c#:

            private TabItem AddTabItem(string tabHeader, string tabName, object tabContent)
            {
                int count = _tabItems.Count;
    
                // create new tab item
                TabItem tab = new TabItem();
    
                tab.Header = tabHeader;
                tab.Name = tabName;
                tab.Tag = _tabItems.Count;
                tab.HeaderTemplate = tabControl.FindResource("TabHeader") as DataTemplate;
    
                // add controls to tab item
                if(tabContent != null)
                tab.Content = tabContent;
    
                // insert tab item 
                _tabItems.Insert(count, tab);
    
                tab.Focus();
    
                return tab;
            } // End Of AddTabItem

     
    2 августа 2013 г. 16:30