简体   繁体   中英

Access Control of DataTemplate - TabItem - DataGrid

I need to generate TabItem N-times, which all need to looks the same. The Tabitem contains a DataGrid, where the ItemsSource needs to be bound on different Items.

I've tried to use a DataTemplate

<DataTemplate x:Key="tabItemContent">
    <TabItem Name="MainTabItem" Header="Main">
        <DataGrid CanUserSortColumns="True" RowDetailsVisibilityMode="Visible" AlternatingRowBackground="#E0E0E0" AlternationCount="2"  CellStyle="{StaticResource BodyContentDataGridCentering }" Name="DgPrinters" AutoGenerateColumns="False" RowHeight="50">
            <!--body content datagrid cell vertical centering-->
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Überwachen" Width="Auto" CellStyle="{StaticResource BodyContentDataGridCentering}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox HorizontalAlignment="Center" IsChecked="{Binding Monitor, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Width="Auto" Header="Druckername" Binding="{Binding FullName, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
                <DataGridTextColumn Width="Auto" Header="Freigabename" Binding="{Binding ShareName, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
                <DataGridTextColumn Header="Portname" Binding="{Binding PortName, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
                <DataGridTextColumn Header="Treibername" Binding="{Binding DriverName, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="Anzahl Jobs: " FontWeight="Bold" />
                        <TextBlock Text="{Binding NumberOfJobs}" Grid.Column="1" />
                        <TextBlock Text="Status: " FontWeight="Bold" Grid.Row="1" Grid.Column="0"/>
                        <TextBlock Text="{Binding Status}" Grid.Row="1" Grid.Column="1" />
                    </Grid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>
    </TabItem>
</DataTemplate>

So I can generate TabItems like

TabItem mainItem = new TabItem();
mainItem.ContentTemplate = TryFindResource("tabItemContent") as DataTemplate;
MainTabControl.Items.Add(mainItem);

Now my Problem is that I need to access the DataGrid of every single TabItem, so I can bind the DataGrid.ItemsSource to different ObservableCollections .

My question now is: Is it possible to access the DataGrids and set the different Sources this way, or am I doing the whole thing the wrong way and there's a better way to achieve all that?

It would be better in that case to have ViewModel for the whole TabControl, which in turn will have ObservableCollecion of VM for each TabItem, and each of TabItem VMs will have own ItemsSource for DataGrid. Below is the code to show the idea:

public class TabControlViewModel : INotifyPropertyChanged
{
    public ObservableCollection<TabItemViewModel> Tabs {get;set;}
    public TabControlViewModel()
    {
        Tabs = new ObservableCollection<TabItem>();
        Tabs.Add(new TabItem { ... });
    }
}

public sealed class TabItemViewModel : INotifyPropertyChanged
{
    public string Header { get; set; }
    public ObservableCollection<DataGridRowVM> DataGridItemsSource {get;set;}
    public TabControlViewModel()
    {
        DataGridItemsSource = new ObservableCollection<DataGridRowVM>();
        DataGridItemsSource .Add(new DataGridRowVM{ ... });
    }
}


public sealed class DataGridRowVM: INotifyPropertyChanged
{
    public string PortName { get; set; }
    public string DriverName{ get; set; }

    .....
}


<TabControl ItemsSource="{Binding Tabs}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Header}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
        <DataGrid CanUserSortColumns="True" RowDetailsVisibilityMode="Visible" ItemsSource="{Binding DataGridItemsSource}>
        <!-- Your template goes here-->

        </DataGrid>

        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM