取消在wpf中为datepicker编辑

时间:2017-11-20 11:54:55

标签: c# wpf datagrid

<DataGrid Name="Tst"  AutoGenerateColumns="False">
    <DataGrid.Columns>
         <DataGridTemplateColumn Header="RecordDate">
               <DataGridTemplateColumn.CellTemplate>
                     <DataTemplate>
                         <DatePicker Text="{Binding RecordDate}" />
                     </DataTemplate>
               </DataGridTemplateColumn.CellTemplate>
         </DataGridTemplateColumn>            
     </DataGrid.Columns>
</DataGrid>

我有一个按钮启用编辑。只有按下按钮才需要启用日期选择器;否则编辑应该禁用

1 个答案:

答案 0 :(得分:0)

您可以将<DataGridTemplateColumn Header="RecordDate"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <DatePicker Text="{Binding RecordDate}"> <DatePicker.Resources> <Style TargetType="DatePickerTextBox"> <Setter Property="IsReadOnly" Value="True" /> </Style> </DatePicker.Resources> </DatePicker> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 设为只读:

function isEmail(str) { // simple email validation
  return /(.+)@(.+){2,}\.(.+){2,}/.test($.trim(str));
}
function isEmpty(str) { // test for empty string
  return $.trim(str) === "";
}
function validate($div) { // validates any div - will not let you leave the div if error
  var $fields = $div.find("input"), hasError = false;
  $fields.each(function() {
    $(this).removeClass("error")
    hasError = this.name=="pword" && isEmpty(this.value);
    if (hasError) {
      $("#pword").addClass("error").focus();
      return false;
    }  
    hasError = this.name=="email" && (isEmpty(this.value) || !isEmail(this.value));
    if (hasError) {
      $("#email").addClass("error").focus();
      return false;
    }  
    hasError = isEmpty(this.value); // the rest of the fields
    if (hasError) {
      $(this).addClass("error").focus();
      return false;
    }  
  })
  return hasError?false:true;
}
$(function() {

  // validate all divs on submit, but actually only necessary to validate thediv the submit is on  
  $("#myForm").on("submit",function(e) { 
    $(".page").each(function() {
      if (!validate($(this))) {
        e.preventDefault(); // stop submission
        return false;
      }
    });
  });

  $(".nav").on("click", function() {
    var $parent = $(this).closest("div");
    var $nextDiv = $(this).hasClass("next") ? $parent.next() : $parent.prev();
    if (validate($parent)) { // is the div this button is on valid?
      $parent.fadeOut(function() { // fade it out and fade the next one in
        if ($nextDiv.length) {
          $nextDiv.fadeIn()
          for (var i=$(".page").length;i> $nextDiv.index(); i--) {
            $("#bar" + i).css({"background-color": "#D8D8D8"}); // we are going backwards          
          }
          $("#bar" + $nextDiv.index()).css({"background-color": "#38610B"});
        }  
      });
    }
  });
});