How can I change the properties of a DataGridTemplateColumn DataTemplate Element for a specific cell

和自甴很熟 提交于 2019-12-24 06:44:09

问题


I have a problem with my DataGrid I created a while ago. The DataGrid shows informations about project tasks. Now I have to make the start- and end-date column editable. For this purpose I changed the DataGridTextColumn with the following code:

<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>

So far so good. Now the problem is that I have to check each row if the Task is in the right state to have the date changed. If so I have to enable the DatePicker. In the code behind I tried it like this:

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;
         }
      }
}

The problem is that "GetCellContent" return null everytime. So my question is how can I change the DatePicker in every row separately? Thanks in advance!


回答1:


I think you should be able to do it this way:

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

GetCellContent() returns a ContentPresenter which first child is the DatePicker


If you try this it will work:

  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;
  }



回答2:


I have finally solved the issue by creating a bool in the ViewModel and bound it to the isEnabled property of the DatePicker.



来源:https://stackoverflow.com/questions/56851866/how-can-i-change-the-properties-of-a-datagridtemplatecolumn-datatemplate-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!