简体   繁体   English

需要控制DataGridTemplateColumn中文本框的外观

[英]Need to control look of textbox in DataGridTemplateColumn

I have a WPF DataGrid with DataGridTextColumns next to DataGridTemplateColumns. 我在WPF DataGrid的DataGridTemplateColumns旁边有DataGridTextColumns。 The cells in the template columns contain a textbox which I disable when a checkbox is checked. 模板列中的单元格包含一个文本框,当选中一个复选框时,我将其禁用。 My problem is the look/behavior of the cells in these two types of columns do not match when they are being edited. 我的问题是这两种类型的列中的单元格的外观/行为在编辑时都不匹配。 In my XAML below, I have set the background of my textbox in the template column to match the cell it is in, which is the way the textbox in a text column works. 在下面的XAML中,我在模板列中设置了文本框的背景以匹配其所在的单元格,这就是文本列中的文本框的工作方式。 Now I need the textbox in the template column to turn white while being edited, as does the textbox in the text column. 现在,我需要模板列中的文本框在编辑时变为白色,就像文本列中的文本框一样。

Here is my XAML: 这是我的XAML:

<DataGridTextColumn Header="Rh" MinWidth="50" Binding="{Binding HorizontalResistivity, StringFormat=N2, Mode=TwoWay}"/>
<DataGridTemplateColumn Header="Rh min" MinWidth="50" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <TextBox x:Name="Rmin" Text="{Binding HorizontalResistivityMin, StringFormat=N2, Mode=TwoWay}"
                     HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0"
                     Background="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}}"/>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=HorizontalResistivityIsFixed}" Value="True">
                    <Setter TargetName="Rmin" Property="IsEnabled" Value="False" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I have come reasonable close to the answer I need. 我已经接近我需要的答案。 I abandoned the DataGridTemplateColumn and reverted to the DataGridTextColumn I was using before it was determined that the data grid cell had to be disabled. 在确定必须禁用数据网格单元之前,我放弃了DataGridTemplateColumn并还原为我正在使用的DataGridTextColumn。 Here is my XAML: 这是我的XAML:

<DataGridTextColumn Header="Rh min" MinWidth="50" Binding="{Binding HorizontalResistivityMin, StringFormat=N2, Mode=TwoWay}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HorizontalResistivityIsFixed}" Value="True">
                    <Setter Property="IsEnabled" Value="false"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="TextBox">
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

Some explanation is in order. 一些解释是为了命令。 After I got the IsEnabled property correctly bound, I found that the TextBlock in the cell was no longer centered (it was aligned left), hence the ElementStyle section which aligned the TextBlock. 正确绑定IsEnabled属性后,我发现单元格中的TextBlock不再居中(它向左对齐),因此,使TextBlock对齐的ElementStyle部分。 Also during editing the cell contents would shift to the left. 同样,在编辑期间,单元格内容将向左移动。 During editing the cell contents are a TextBox, so I have the EditingElementStyle section which aligns the TextBox. 在编辑期间,单元格的内容是一个TextBox,所以我有EditingElementStyle部分,它使TextBox对齐。

That leave only one issue, while editing any TextBox in this column, the whole row expands in height, maybe 10%. 这只剩下一个问题,在编辑此列中的任何TextBox时,整个行的高度都会扩大,可能会增加10%。 This does not happen in any DataGridTextColumn that does not have the disabling feature. 在没有禁用功能的任何DataGridTextColumn中都不会发生这种情况。 It also does not happen if I don't include the EditingElementStyle section. 如果我不包括EditingElementStyle部分,也不会发生。 It seems that adding the DataGridTextColumn.CellStyle section messed with some defaults, and getting them all beck is a pain. 似乎添加DataGridTextColumn.CellStyle节与一些默认值混为一谈,而让所有默认值都让人感到痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM