如何绑定自定义datagrid列

时间:2017-02-11 12:41:01

标签: c# wpf xaml datagrid dependency-properties

我需要修改datagrid,以便每个列标题都有一个文本框,组合框和复选框,所有这些都必须绑定到属性。我花了很多时间尝试使用默认的DataGrid来实现它,但我不认为这是可能的,所以我决定创建一个自定义的DataGrid。

到目前为止,我有一个存储.as-console-wrapper { max-height: 100% !important; top: 0; }的可绑定属性See this its not overlapping i have changed the id # **hitchhacking-info to float: left;** #intro{ font-family: 'Sansita', sans-serif; font-size: 170%; float: right; width: 50%; margin-right: 20px; margin-left: 10px; color: #ff471a ; } #introImage { float: left; width: 40%; margin-left: 70px; margin-top: 35px; box-shadow: 10px 10px grey; border-radius: 10px; } #hitchhiking-info { margin-top: 20px; width: 100%; height: 100px; text-align: center; background-color: blue; float: left; } ,所以我有我需要显示的数据。问题是我不知道如何将这些数据传递给BindableColumns,因此我可以将列添加到DataTable的{​​{1}}属性。

OnAutoGeneratedColumns

和xaml:

Columns

修改

感谢@Ramin我有工作专栏。我在动态添加行时遇到了一些麻烦,因为DataGrid期望行作为一个类与列的绑定具有完全相同的变量名。对于任何有问题的人,我都是这样解决的:

public class BindableGrid : DataGrid
{
    public DataTable BindableColumns
    {
        get { return (DataTable)GetValue(BindableColumnsProperty); }
        set { SetValue(BindableColumnsProperty, value); }
    }

    public static readonly DependencyProperty BindableColumnsProperty =
        DependencyProperty.Register("BindableColumns", typeof(DataTable), typeof(BindableGrid), new PropertyMetadata(null, BindableColumnsPropertyChanged));

    private static void BindableColumnsPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        // This is where I get DataTable after binding
    }


    protected override void OnAutoGeneratedColumns(EventArgs e)
    {
        // This is where I need the DataTable to generate columns.
        // I don't know how to invoke this method myself.
        Columns.Add(new DataGridTemplateColumn
        {
            Header = "Test1",
            HeaderTemplate = new DataTemplate()
        });
    }
}

1 个答案:

答案 0 :(得分:1)

关于问题的根源,这里是一个带有模板化头的DataGrid,它包含一个TextBox,ComboBox和一个CheckBox:

$timeout

DataContext使用自定义类:

 <DataGrid >
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding}" >
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBox Text="{Binding DataContext.Name, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
                            <CheckBox IsChecked="{Binding DataContext.Value , RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                            <ComboBox ItemsSource="{Binding DataContext.Names , RelativeSource={RelativeSource AncestorType=DataGrid}}" SelectedIndex="0"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

修改

您可以动态添加列:

public MainWindow()
{
    InitializeComponent();
    DataContext = new MyClass() { Name = "Name0", Value = true, Names = new string[2] { "Name1", "Name2" } };
}

public class MyClass
{
    public string Name { get; set; }
    public bool Value { get; set; }
    public string[] Names { get; set; }
}

在App.xaml中:

    public void addNewColumn(Header h, string bindcol)
    {
        DataGridColumn col = new DataGridTextColumn(){Binding=new Binding(bindcol)};
        col.Header = h;
        col.HeaderTemplate = (DataTemplate)FindResource("dgh") as DataTemplate;
        dg.Columns.Add(col);
    }

要测试(假设有一个名为 dg 的DataGrid):

<Application.Resources>
    <DataTemplate x:Key="dgh">
        <StackPanel Orientation="Horizontal">
            <TextBox Text="{Binding Name}" />
            <CheckBox IsChecked="{Binding Value}"/>
            <ComboBox ItemsSource="{Binding Names}" SelectedIndex="0"/>
        </StackPanel>
    </DataTemplate>
</Application.Resources>

请注意&#34; col1&#34;和&#34; col2&#34;请参阅datagrid的ItemsSource。