如何在 TextBlock WPF DataGridTextColumn 的编辑模式下换行文本
How to wrap text while in edit mode on TextBlock WPF DataGridTextColumn
我有一个 DataGridTextColumn,我在其中将文本包装在一个 column.Since 只有一个单元格中有多个数字,对于这种情况,电缆名称在 3 行中显示正常,但在编辑模式下包装会离开,我看不到整个文本。需要使用箭头键。我想在编辑模式下有一个可编辑和文本环绕的 texblock while。在 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>
幸运的是,解决方案很简单:
将 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 -->
除了 TextBlock
的 ElementStyle
之外,您还应该定义一个 EditingElementStyle
来为 TextBox
设置 TextWrapping
属性到 Wrap
:
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
当您处于编辑模式时,没有 TextBlock
显示。这是 TextBox
.
我有一个 DataGridTextColumn,我在其中将文本包装在一个 column.Since 只有一个单元格中有多个数字,对于这种情况,电缆名称在 3 行中显示正常,但在编辑模式下包装会离开,我看不到整个文本。需要使用箭头键。我想在编辑模式下有一个可编辑和文本环绕的 texblock while。在 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>
幸运的是,解决方案很简单:
将 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 -->
除了 TextBlock
的 ElementStyle
之外,您还应该定义一个 EditingElementStyle
来为 TextBox
设置 TextWrapping
属性到 Wrap
:
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
当您处于编辑模式时,没有 TextBlock
显示。这是 TextBox
.