2016-10-09 8 views
0

行の異なる項目を編集可能または読み取り専用にできるDataGridがあります。テンプレート内の要素のバインディングを変更する

私は意志で読取り専用にする必要が単一のセルを持っていた場合、私は

<DataGrid.Resources> 
    <!-- the non-editing cell --> 
    <DataTemplate x:Key="ReadonlyCellTemplate"> 
     <TextBlock Text="{Binding UserName}" /> 
    </DataTemplate> 

    <!-- the editing cell --> 
    <DataTemplate x:Key="EditableCellTemplate"> 
     <TextBox Text="{Binding UserName}" /> 
    </DataTemplate> 
</DataGrid.Resources> 

そして、私は私の選択の欄にそのテンプレートを適用するよりものようなものを使用すると思います。

<DataGridTemplateColumn CellTemplate="{StaticResource ReadonlyCellTemplate}" Header="User name"> 
    <DataGridTemplateColumn.CellEditingTemplate> 
     <DataTemplate> 
      <!-- the additional layer of content presenter --> 
      <ContentPresenter x:Name="Presenter" Content="{Binding}" ContentTemplate="{StaticResource ReadonlyCellTemplate}" /> 
      <DataTemplate.Triggers> 
       <!-- dynamically switch the content template by IsEditable binding --> 
       <DataTrigger Binding="{Binding CreationFieldsEditable}" Value="True"> 
        <Setter TargetName="Presenter" Property="ContentTemplate" Value="{StaticResource EditableCellTemplate}" /> 
       </DataTrigger> 
      </DataTemplate.Triggers> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellEditingTemplate> 
</DataGridTemplateColumn> 

私は、テンプレート内の{Binding UserName}を変更できるようにしたいので、私は別の列にテンプレートを適用することができます。

どうすればよいですか?

+0

Text = "{Binding UserName1}"の代わりにText = "{Binding UserName2}"などのバインディングプロパティを変更する場合は、データトリガーで要素自体を変更するのが普通です。 条件をよく理解していれば、解決策の詳細を説明することができます。 –

答えて

2

代わりの

<DataTemplate x:Key="EditableCellTemplate"> 
    <TextBox Text="{Binding UserName}" /> 
</DataTemplate> 

テンプレートを拡張する必要があります。

<DataTemplate x:Key="t1"> 
     <TextBox Text="{Binding UserName1}" /> 
    </DataTemplate> 
    <DataTemplate x:Key="t2"> 
     <TextBox Text="{Binding UserName2}" /> 
    </DataTemplate> 
    <DataTemplate x:Key="EditableCellTemplate"> 
     <ContentPresenter x:Name="ctp" /> 

     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding MyProperty}" 
         Value="1"> 
       <Setter TargetName="ctp" 
         Property="ContentTemplate" 
         Value="{StaticResource t1}" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding MyProperty}" 
         Value="2"> 
       <Setter TargetName="ctp" 
         Property="ContentTemplate" 
         Value="{StaticResource t2}" /> 
      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 

私はよくあなたのアイデアを理解する場合、これは動作するはずです。

関連する問題