2015-10-01 108 views
0

在我的應用程序中,我有一個UserControlDataGrid。一位在DataGrid細胞的定義如下:DataGridCell在ViewModel中設置編輯模式

<DataGridTemplateColumn Header="Alternative path" Width="Auto" MinWidth="60" SortMemberPath="OtherModulePath"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding OtherModulePath}" Style="{StaticResource DataGridTextBlockStyle}"/> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
     <DataTemplate> 
      <TextBox Text="{Binding OtherModulePath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Padding="4,1"/> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellEditingTemplate> 
    <DataGridTemplateColumn.CellStyle> 
     <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource BaseDataGridCellStyle}"> 
      <Setter Property="AutomationProperties.Name" 
        Value="{Binding OtherModulePath, 
            Converter={Converter:AutomationPropertiesNameFromEmptyToSpaceConverter}, 
            FallbackValue=' '}"/> 
     </Style> 
    </DataGridTemplateColumn.CellStyle> 
</DataGridTemplateColumn> 

如果我選擇單元格,與Editing- TextBox顯示以及鼠標左鍵再次點擊。

DataGrid以上有Button。點擊這個ButtonDataGridCell我已經顯示應該切換到編輯模式,這樣OtherModulePath的TextBox應該是可見的。

我該怎麼用MVVM做到這一點?

我知道如何使用代碼隱藏功能,但我不知道如何重新開始數據綁定。

回答

0

DataGrid中有一個名爲BeginEditCommand命令,您可以將按鈕的命令綁定到像這樣的命令:

//suppose your grid has name of dg 
<Button Command="{x:Static DataGrid.BeginEditCommand}" 
     CommandTarget="{Binding ElementName=dg}"> 
    <!-- ... --> 
</Button> 
相關問題