DataTemplate的访问控制 - TabItem - DataGrid

时间:2016-02-01 10:10:06

标签: c# wpf xaml datatemplate

我需要生成TabItem N次,所有这些都需要看起来相同。 Tabitem包含一个DataGrid,其中ItemsSource需要绑定在不同的Items上。

我尝试过使用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>

所以我可以生成像

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

现在我的问题是我需要访问每个TabItem的DataGrid,因此我可以将DataGrid.ItemsSource绑定到不同的ObservableCollections

我现在的问题是:是否可以访问DataGrids并以这种方式设置不同的Sources,或者我是以错误的方式完成整个事情并且有更好的方法来实现所有这些?

1 个答案:

答案 0 :(得分:1)

在这种情况下,为整个TabControl提供ViewModel会更好,而TabControl又为每个TabItem提供VM的ObservableCollecion,每个TabItem VM都有自己的DataGrid ItemsSource。以下是显示该想法的代码:

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>