HeaderTemplate中DataGrid中的WPF [英] HeaderTemplate in DataGrid WPF

查看:606
本文介绍了HeaderTemplate中DataGrid中的WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有一种一个datepicker在我的DataGrid标题为列。当用户从该头日期选择的时间,该系统应结合该日期的所有列的单元的日期。

有没有办法做到这一点?

I need to have a kind of a datepicker in my datagrid header for one of the columns. When the user selects the date from this header datepicker, the system should bind this date to all the column cells with the date.
Is there a way to do it?

推荐答案

最好的办法就是到标题的DataTemplate中设置为包含一个datepicker的日期是绑定到DataGrid的的DataContext的一个属性的自定义模板,然后绑定细胞这个特定的列相同的属性。

the best way is to set the header's dataTemplate to a custom template containing a DatePicker whose Date is bound to one of the DataGrid's DataContext's properties, then bind the cells in this specific column to the same property.

是这样的:

    <DataGrid>
        <DataGridTextColumn Binding="{Binding DataContext.myDate, RelativeSource={RelativeSource AncestorType=DataGrid}, Mode=OneWay}" >
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Style.Setters>
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <DatePicker SelectedDate={Binding myDate, Mode=TwoWay} />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style.Setters>
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
    </DataGrid>

免责声明:我没有尝试这一点,我不知道有关 {结合DataContext.myDate,的RelativeSource = {的RelativeSource AncestorType = DataGrid中} 的事情。你可能将不得不做一些调整,但总体而言,这应该给你如何进行启动

Disclaimer: I did not try this and am not sure about the {Binding DataContext.myDate, RelativeSource={RelativeSource AncestorType=DataGrid} thing. You would probably have do to some adjustments, but overall, this should give you a start on how to proceed

这篇关于HeaderTemplate中DataGrid中的WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆