关于 DataGridTextColumn的IsReadOnly

1. 以下是绑定方式,但是IsReadOnly不起作用

<DataGrid x:Name="dgTest" ItemsSource="{Binding}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" IsReadOnly="{Binding Path=Pass}"  Binding="{Binding Path= Name}"/>
</DataGrid.Columns>
<DataGrid>

2. IsReadOnly不是针对某个cell的约定,而是对整列起作用,所以这样的绑定有问题。

3. 可以替换成以下代码:

DataGridTextColumn 换为 DataGridTemplateColumn 
<DataGridTemplateColumn Header="Name">
          <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate>
                            <TextBlock Text="{Binding Path=Name,Mode=OneWay}" />
                   </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
           <DataGridTemplateColumn.CellEditingTemplate>
                   <DataTemplate>
                            <TextBox Text="{Binding Path=Name,Mode=TwoWay}" IsReadOnly="{Binding Path=Pass}"/>
                  </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
 </DataGridTemplateColumn>

 4. 双击进入编辑界面的时候TextBox没有默认聚焦鼠标光标。修改如下:

FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"
      <DataTemplate x:Key="cellEditingTemplate">
                <local:MultiLineTextBox x:Name="multiLineTxt" Text="{Binding Description}"/>
                <DataTemplate.Triggers>
                    <Trigger SourceName="multiLineTxt" Property="IsVisible" Value="True">
                        <Setter TargetName="multiLineTxt" Property="FocusManager.FocusedElement" Value="{Binding ElementName=multiLineTxt}"/>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>

OR

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <StackPanel>
            <TextBox x:Name="editCommentTextBox" Text="{Binding Comment, Mode=TwoWay}"
                     FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}">
            </TextBox>
         </StackPanel>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

 5.cell添加背景颜色CellStyle="{StaticResource  dgCellStyle}" 

       <Style x:Key="dgCellStyle"  TargetType="DataGridCell">
                <Setter Property="Background" Value="#DDFFDE"/>
            </Style>

 6. 根据是否只读决定单元格的北京颜色

            <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Style.Triggers>
                                <DataTrigger  Binding="{Binding Path=IsReadOnly}" Value="False">
                                    <Setter Property="Background" Value="#DDFFDE"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>

 

 

 

参考:

http://www.cnblogs.com/fwbnet/archive/2012/05/08/2490974.html

http://www.cnblogs.com/zhcw/archive/2013/01/12/2857433.html

posted @ 2016-06-14 15:33  亲福  阅读(1451)  评论(0编辑  收藏  举报