WPF datagrid header text binding

后端 未结 9 2296
执念已碎
执念已碎 2020-11-27 18:01

The column header of the DataGrid is not a FrameWork element for some reason, and so you cannot use bindings to set things like the header text. Please correct me if that is

9条回答
  •  忘掉有多难
    2020-11-27 18:37

    **EDIT :-

    You can style the DataGridColumnHeader and do some funky bindings. try here and download the ColumnHeaderBindings.zip, it has a little test project, that is a bit of a hack, but it works

    **End Edit

    The Binding on the column happens on a per row basis, the column is not part of the visual tree, the binding gets applied to each item in the grid, from the grids source code you can see that the property Binding has these comments

        /// 
        ///     The binding that will be applied to the generated element.
        /// 
        /// 
        ///     This isn't a DP because if it were getting the value would evaluate the binding.
        /// 
    

    So binding to the columns does not make much sense, because as you have found out, when you are not part of the visual tree you have no data context.

    The same problem exists with the ComboBoxColumn when you want to bind to the items source. You can bind to a StaticResource, but StaticResources dont have a data context either. You could use an object data provider or just instantiate directly in xaml.

    but i would just create the columns in code, and set the header. this problem would just go away then.

    there is a good article here on the visual layout.

提交回复
热议问题