如何绑定DataGrid标头

时间:2012-09-23 19:33:35

标签: .net wpf datagrid

无法让ComboBox中的DataGridTextColumn.Header绑定 第一个独立的ComboBox绑定得很好 字段只是Public List<String>

<StackPanel Orientation="Vertical">
    <ComboBox ItemsSource="{Binding Path=Fields}"/>
    <DataGrid ItemsSource="{Binding Path=MyRows}" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <DataGridTextColumn Width="80" Binding="{Binding Col1}">
                <DataGridTextColumn.Header>
                    <StackPanel>
                        <TextBlock Text="Header1" />
                        <ComboBox Width="60" ItemsSource="{Binding Path=Fields}" />
                    </StackPanel>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>
private List<string> fields = new List<string> { "aa", "bb", "cc" };
public List<string> Fields { get { return fields; } }

甚至尝试将Fields作为MyRow的属性

1 个答案:

答案 0 :(得分:2)

如果你在DataGrid上使用DataContext,它应该可以工作,它继承自Window。

<ComboBox Width="60"
          ItemsSource="{Binding Path=DataContext.Fields, 
          RelativeSource={RelativeSource AncestorType=DataGrid}}" />