如何在TextBlock WPF DataGridTextColumn上的编辑模式下换行

时间:2019-04-03 06:11:35

标签: c# wpf datagrid wpfdatagrid

我有一个DataGridTextColumn,其中将文本包装在一列中,因为只有一个单元格的数目超过了数字,在这种情况下,电缆名称显示为3行很好,但是在编辑模式下,包装自动消失了,我没有看全文。需要使用箭头键。我想在编辑模式下同时拥有一个可编辑和文本包装的texblock。寻找有关SO的建议,但没有成功。 有帮助吗?

CableEditorWindow.xaml:

    <Controls:MetroWindow x:Class="GPZmodel.Windows.CableEditorWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="Edytor właściwości kabla" Height="664" Width="610" 
    Background="WhiteSmoke" WindowStartupLocation="CenterScreen">

<Grid>



    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto" MinHeight="70"/>
    </Grid.RowDefinitions>
    <DataGrid  x:Name="CableEditDataGrid"
               Style="{StaticResource AzureDataGrid}"
               Grid.Row="0" 
               VerticalAlignment="Stretch" 
               ItemsSource="{Binding Rows, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
               CanUserAddRows="False" 
               CanUserDeleteRows="False" CanUserSortColumns="True" AutoGenerateColumns="False"
               ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" 
               SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Pole" Width="Auto" 
                                HeaderStyle="{StaticResource WysrodkujHeader}" 
                                Binding="{Binding nazwa_pola,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                                IsReadOnly="True" 
                                >
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <Trigger Property="Text"  Value="ID">
                                <Setter Property="Background" Value="#FF84BCCD"/>
                                <Setter Property="FontSize" Value="13"/>
                            </Trigger>
                        </Style.Triggers>
                        <Setter Property="TextAlignment" Value="Center"/>

                    </Style>
                </DataGridTextColumn.ElementStyle>

            </DataGridTextColumn>


            <!--THIS-->
            <DataGridTextColumn Header="Wartość" 
                                HeaderStyle="{StaticResource WysrodkujHeader}"
                                Width="*" 
                                Binding="{Binding wartosc_pola, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                > 
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=ReadOnly}" Value="True">
                                <Setter Property="IsTabStop" Value="False"/>
                                <Setter Property="Focusable" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                        <Style.Setters>
                            <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                            <Setter Property="VerticalAlignment" Value="Stretch"/>
                            <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        </Style.Setters>
                    </Style>
                </DataGridTextColumn.CellStyle>
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <!--/THIS-->

            <DataGridTextColumn x:Name="JednostkaTextBox" 
                                Header="Jednostka"
                                HeaderStyle="{StaticResource WysrodkujHeader}"
                                Binding="{Binding jednostka_pola,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                IsReadOnly="True"
                                Width="0.4*">
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
    <Grid Grid.Row="1" Background="#4A6A95">
        <Button Style="{StaticResource MainButtonStyle}" Content="Nadpisz" x:Name="NadpiszButton"   HorizontalAlignment="Right" Width="120" Panel.ZIndex="1" Margin="0,0,10,21" Height="32" VerticalAlignment="Bottom" Click="NadpiszButton_Click" />
        <Button Style="{StaticResource MainButtonStyle}" Content="Anuluj" Name="AnulujButton" Height="32" VerticalAlignment="Bottom" Panel.ZIndex="1" Margin="0,0,135,21" HorizontalAlignment="Right" Width="120" Click="AnulujButton_Click" />
    </Grid>
</Grid>

2 个答案:

答案 0 :(得分:0)

幸运的是,解决方案很简单: 将EditingElementStyle添加到您的列中,您可以(或应该)分别自定义编辑样式

<DataGridTextColumn.EditingElementStyle>
   <Style>
      <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
   </Style>
</DataGridTextColumn.EditingElementStyle>

您最终的代码部分将如下所示

<!--  THIS  -->
<DataGridTextColumn Width="*"
    Binding="{Binding wartosc_pola, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    Header="Wartość"
    HeaderStyle="{StaticResource WysrodkujHeader}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=ReadOnly}" Value="True">
                    <Setter Property="IsTabStop" Value="False" />
                    <Setter Property="Focusable" Value="False" />
                </DataTrigger>
            </Style.Triggers>
            <Style.Setters>
                <Setter Property="TextBlock.TextAlignment" Value="Center" />
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="HorizontalAlignment" Value="Stretch" />
            </Style.Setters>
        </Style>
    </DataGridTextColumn.CellStyle>
    <DataGridTextColumn.ElementStyle>
        <Style>
            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
        </Style>
    </DataGridTextColumn.ElementStyle>
    <DataGridTextColumn.EditingElementStyle>
        <Style>
            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<!--  /THIS  -->

答案 1 :(得分:0)

除了for c in df.select_dtypes(object).columns: if pd.to_numeric(df[c], errors='coerce').notna().any(): print (c) 的{​​{1}},还应该定义一个ElementStyle,将TextBlock的{​​{1}}属性设置为EditingElementStyle

TextWrapping

在编辑模式下,没有显示TextBox。这是Wrap