从datagrid内部引用datepicker seleteddate

时间:2012-07-26 13:42:52

标签: wpf datagrid datepicker

编程新手,需要一些帮助。我有一个用于Outlook加载项的WPF用户控件,一切都在游泳,直到我在celleditingtemplate列中尝试使用datepicker的数据网格,但是celltemplatecolumn只有一个文本块。我们的想法是将文本块绑定到数据源(工作正常),当用户编辑列时,日期选择器显示,用户可以选择日期。我根本无法弄清楚如何将值从selecteddate发送到textblock并且我完全在我的智慧结束。下面是带有绑定的datagrid,带有以下字段的简单SQL表,[Project],[Deadline],[DaystoDeadline],[Responibility],[ProcessCount],[TasksCompleted],[TasksRemaining]。非常感谢任何帮助。

            <Grid>
                <DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="6,0,0,0" Name="ProjectsGrid" ScrollViewer.CanContentScroll="True" VerticalAlignment="Top" VerticalScrollBarVisibility="Auto" ItemsSource="{Binding}" CanUserAddRows="False" CanUserDeleteRows="False">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Commands">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                            <Button Click="Select_Button" Content="Select" Name="SelectButton" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTextColumn Binding="{Binding Project}" Header="Project" IsReadOnly="True" FontFamily="Verdana" />
                            <DataGridTemplateColumn Header="Deadline">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Deadline, StringFormat='yyyy/MM/dd'}" FontFamily="Verdana" x:Name="DeadlineTxt" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <DatePicker SelectedDate="{Binding Deadline, StringFormat='yyyy/MM/dd'}" FontFamily="Verdana" x:Name="DeadlineDatePicker">
                                            <DatePicker.CalendarStyle>
                                                <Style TargetType="Calendar">
                                                    <Setter Property="DisplayMode" Value="Month"/>
                                                </Style>
                                            </DatePicker.CalendarStyle>
                                        </DatePicker>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>
                        <DataGridTextColumn Binding="{Binding DaystoDeadline}" Header="Days to Deadline" IsReadOnly="True" FontFamily="Verdana" />
                        <DataGridTextColumn Binding="{Binding Responsibility}" Header="Responsibility" IsReadOnly="True" FontFamily="Verdana" />
                        <DataGridTextColumn Binding="{Binding Processes}" Header="Processes" IsReadOnly="True" FontFamily="Verdana" />
                        <DataGridTextColumn Binding="{Binding Completed}" Header="Completed" IsReadOnly="True" FontFamily="Verdana" />
                        <DataGridTextColumn Binding="{Binding Remaining}" Header="Remaining" IsReadOnly="True" FontFamily="Verdana" />
                    </DataGrid.Columns>
                </DataGrid>

1 个答案:

答案 0 :(得分:0)

我首先取消DataTemplatex:Name="DeadlineTxt" x:Name="DeadlineDatePicker")中控件的名称,然后从StringFormat中移除SelectedDate :( SelectedDate="{Binding Deadline}"

            <DataGridTemplateColumn Header="Deadline">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Deadline, StringFormat='yyyy/MM/dd'}" FontFamily="Verdana" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <DatePicker SelectedDate="{Binding Deadline}" FontFamily="Verdana" >
                            <DatePicker.CalendarStyle>
                                <Style TargetType="Calendar">
                                    <Setter Property="DisplayMode" Value="Month"/>
                                </Style>
                            </DatePicker.CalendarStyle>
                        </DatePicker>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>