绑定仅适用于第一个TabItem

时间:2014-12-24 10:36:34

标签: c# wpf datagrid tabcontrol

我尝试更改DataGridColumnHeader,它取决于Combobox值,但它只适用于第一个tabItem。如果我重新排序列 - 它将仅适用于第一个tabItem。我该怎么办?我的XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.15*"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <ComboBox Margin="10" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}"/>
    <TabControl Grid.Row="1">
        <TabItem Header="Second">
            <DataGrid AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.Header>
                            <TextBlock Text="{Binding DataContext.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
                        </DataGridTemplateColumn.Header>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </TabItem>
        <TabItem Header="First">
            <DataGrid AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.Header>
                            <TextBlock Text="{Binding DataContext.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
                        </DataGridTemplateColumn.Header>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </TabItem>
    </TabControl>
</Grid>

代码示例here

1 个答案:

答案 0 :(得分:1)

标头主要用于提供硬编码值。

使用HeaderTemplate:

   <TabControl Grid.Row="1">
        <TabItem Header="Second">
            <DataGrid AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding DataContext.SelectedItem, RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType=Window}}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.HeaderTemplate>

                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </TabItem>
        <TabItem Header="First">
            <DataGrid AutoGenerateColumns="False">
                <DataGrid.Columns>                                              
                    <DataGridTemplateColumn>
                         <DataGridTemplateColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding DataContext.SelectedItem, RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType=Window}}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.HeaderTemplate>                            
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </TabItem>
    </TabControl>