如何更改特定单元格的datagridTemplateColumn数据置元素的属性[英] How can I change the properties of a DataGridTemplateColumn DataTemplate Element for a specific cell

本文是小编为大家收集整理的关于如何更改特定单元格的datagridTemplateColumn数据置元素的属性的处理方法,想解了如何更改特定单元格的datagridTemplateColumn数据置元素的属性的问题怎么解决?如何更改特定单元格的datagridTemplateColumn数据置元素的属性问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我在不久前创建的数据杂志有问题. DataGrid显示了有关项目任务的信息.现在,我必须使起点和末期列可编辑.为此,我使用以下代码更改了DataGridTextColumn:

<DataGridTemplateColumn Header="Start"  Width="100" CanUserReorder="True">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <DatePicker 
        x:Name="DpStartDate" 
        SelectedDate="{Binding PlannedStart, StringFormat=\{0:dd.MM.yyyy\}}" 
        IsEnabled="False"/>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

到目前为止一切都很好.现在的问题是,如果任务处于正确状态以更改日期,我必须检查每行.如果是这样,我必须启用datepicker.在背后的代码中,我尝试这样的代码:

for(int _i = 0; _i < DgvProjectTaskList.Items.Count; _i++)
{
      switch (((TaskListViewModel) DgvProjectTaskList.Items[_i]).Task.State)
      {
         case TaskStates.InWork:
         {
            DatePicker _dpStart = DgvProjectTaskList.Columns[4].GetCellContent(_i) as DatePicker;
            _dpStart.IsEnabled = false;
            DatePicker _dpEnd = DgvProjectTaskList.Columns[5].GetCellContent(_i) as DatePicker;
            _dpEnd.IsEnabled = true;
            break;
         }
         case TaskStates.Done:
         {
            DatePicker _dpStart = DgvProjectTaskList.Columns[4].GetCellContent(_i) as DatePicker;
            _dpStart.IsEnabled = false;
            DatePicker _dpEnd = DgvProjectTaskList.Columns[5].GetCellContent(_i) as DatePicker;
            _dpEnd.IsEnabled = false;
            break;
         }
         default:
         {
            DatePicker _dpStart = DgvProjectTaskList.Columns[4].GetCellContent(_i) as DatePicker;
            _dpStart.IsEnabled = true;
            DatePicker _dpEnd = DgvProjectTaskList.Columns[5].GetCellContent(_i) as DatePicker;
            _dpEnd.IsEnabled = true;
            break;
         }
      }
}

问题是" getCellContent"每次返回null.因此,我的问题是如何分别更改每行的datepicker? 预先感谢!

推荐答案

我认为您应该能够这样做:

var contentPresenter = DgvProjectTaskList.Columns[4].GetCellContent(item);
var datePicker = VisualTreeHelper.GetChild(contentPresenter, 0) as DatePicker;
datePicker.IsEnabled = true;

GetCellContent()返回ContentPresenter哪个孩子是DatePicker


如果您尝试过,它将起作用:

  foreach (var item in DgvProjectTaskList.Items)
  {
    bool enable = item.Task.State != TaskStates.Done;

    var contentPresenter = Data.Columns[4].GetCellContent(item );
    var picker = VisualTreeHelper.GetChild(contentPresenter, 0) as DatePicker;
    picker.IsEnabled = enable;
    contentPresenter = Data.Columns[5].GetCellContent(item );
    picker = VisualTreeHelper.GetChild(contentPresenter, 0) as DatePicker;
    picker.IsEnabled = enable;
  }

其他推荐答案

我终于通过在ViewModel中创建Bool并将其绑定到DatePicker的IseNabled属性来解决问题.

本文地址:https://www.itbaoku.cn/post/1794109.html