2012-04-23 30 views
1

我有DataGrid,如果我想在單元格中編輯值,我必須對此執行雙擊,並在這裏出現光標(只需點擊一下就可以選擇合適的單元格)。EditMode在DataGrid中通過單擊XAML觸發器

我可以通過Xaml觸發器單擊單元格,而不是隻選中它們,但是一次輸入EditMode,當我用箭頭在單元格之間切換時,它們也會在EditMode中輸入?

這裏我現在修改後的代碼

<Page.Resources> 
    <grd:LenghthToVisibility x:Key="LenghthToVisibility"/> 
    <grd:StringToSystemIconConverter x:Key="StringToSystemIconConverter"/> 
    <grd:booleanConverter x:Key="booleanConverter"/> 
    <DataGrid.CellStyle> 
     <Style TargetType="{x:Type DataGridCell}"> 
      <Setter Property="IsTabStop" Value="False" /> 
      <Setter Property="Focusable" Value="False" /> 
     </Style> 
     <Style x:Key="RightCellStyle" TargetType="DataGridCell"> 
     <Setter Property="HorizontalAlignment" Value="Right" /> 
    </Style> 
    <Style x:Key="RightAlignedCell" TargetType="{x:Type DataGridCell}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type DataGridCell}"> 
        <Grid Background="{TemplateBinding Background}"> 
         <ContentPresenter HorizontalAlignment="Right" VerticalAlignment="Center"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="DataGridCell.IsSelected" Value="True"> 
       <Setter Property="IsEditing" Value="True" /> 
       <Setter Property="Background" Value="#356815" /> 
       <Setter Property="Foreground" Value="#e2fce2" /> 
      </Trigger> 
      </Style.Triggers> 
    </Style> 
     </DataGrid.CellStyle> 
    </Page.Resources> 

感謝。

我有兩個奇怪的錯誤,並刷新我上面的代碼:「錯誤5的附着性能‘CellStyle’在類型‘數據網格’未找到 2)」 1)錯誤2標籤‘DataGrid.CellStyle’呢在XML命名空間 'schemas.microsoft.com/winfx/2006/xaml/presentation' 不存在」

回答

1

要忽略DataGridCell(重點內容)使用方法:

<DataGrid.CellStyle> 
    <Style TargetType="{x:Type DataGridCell}"> 
     <Setter Property="IsTabStop" Value="False" /> 
     <Setter Property="Focusable" Value="False" /> 
    </Style> 
</DataGrid.CellStyle> 

要在ElementStyle進入編輯模式/ EditingElementStyleCellTemplate/CellEditingTemplate zh環境組DataGridCell.IsEditing Property爲真如果選擇:

<Style x:Key="RightAlignedCell" TargetType="{x:Type DataGridCell}"> 
    ... 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="IsEditing" Value="True" /> 
      ... 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

非常好,乾淨! – 2012-04-24 04:43:51

+0

謝謝,但由於某種原因,我有一個錯誤,「錯誤的附着性能‘CellStyle’沒有在類型‘數據網格’發現\t 。」 – Denisko 2012-04-26 15:40:02

+0

也許在你的XAML像[這]一個錯字(HTTP:/ /stackoverflow.com/questions/6360692/xaml-the-attachable-property-columns-was-not-found-in-type-datagrid)? – LPL 2012-04-26 17:04:40