TemplateBinding ReadOnly from DataGridCell

本文关键字:DataGridCell from ReadOnly TemplateBinding | 更新日期: 2023-09-27 18:04:21

这是我的DataGrid

<DataGrid Name="OrderItemList" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Code" Binding="{Binding Code}" IsReadOnly="True" />
        <DataGridTextColumn Header="Price" Binding="{Binding Price`}" IsReadOnly="True" />
        <DataGridTextColumn Header="Qty" Binding="{Binding Qty}" />
        <DataGridTextColumn Header="" Width="*"/>
    </DataGrid.Columns>
</DataGrid>

这是我的风格

<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="DataGridCell.IsSelected" Value="True">
            <Setter Property="Background" Value="#F1F1F1" />
        </Trigger>
    </Style.Triggers>
    <Setter Property="Padding" Value="10" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text, Mode=TwoWay, UpdateSourceTrigger=Default}"
                 HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}"
                 BorderThickness="0" IsReadOnly="...." />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如何绑定DataGridTextColumn。IsReadOnly to TextBox.IsReadOnly?

TemplateBinding ReadOnly from DataGridCell

DataGridCell有一个名为Column的属性,该属性返回单元格所属的列。

试试IsReadOnly="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Column.IsReadOnly, Mode=OnTime}"