简体   繁体   English

DataTemplate的访问控制-TabItem-DataGrid

[英]Access Control of DataTemplate - TabItem - DataGrid

I need to generate TabItem N-times, which all need to looks the same. 我需要生成N次TabItem,它们都需要看起来相同。 The Tabitem contains a DataGrid, where the ItemsSource needs to be bound on different Items. Tabitem包含一个DataGrid,其中ItemsSource需要绑定到不同的Items上。

I've tried to use a DataTemplate 我试图使用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 所以我可以生成TabItems

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 . 现在我的问题是我需要访问每个TabItem的DataGrid,因此可以将DataGrid.ItemsSource绑定到不同的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? 我现在的问题是:是否可以通过这种方式访问​​DataGrid并设置不同的Sources,还是我以错误的方式来做整个事情,并且有更好的方法来实现所有这些?

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. 在这种情况下,最好为整个TabControl使用ViewModel,然后为每个TabItem使用VM的ObservableCollecion,并且每个TabItem VM将为DataGrid拥有自己的ItemsSource。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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