Textblock textwrapping无法正常工作

时间:2012-06-13 20:41:31

标签: wpf textblock textwrapping datagridtemplatecolumn

有人可以让我知道为什么包装不在这些Textblocks中工作吗? 我只是不知道为什么,我把所有的stackpanels都改成了网格,以避免无限的空间问题,但它仍然不起作用......

<Border x:Name="SummaryRightSideContainerBorder" Grid.Column="1"
        Margin="10,10,10,10" BorderBrush="Black"
        BorderThickness="1">
    <ContentControl x:Name="SummaryRightSideContainerContentControl" Content="{Binding SelectedItem, ElementName=SummaryTreeView}">
        <ContentControl.Resources>
            <DataTemplate DataType="{x:Type ViewModelsProject:ProjectViewModel}">
                <StackPanel>
                    <TextBlock Text="Displaying Project DataContract stuff..." />
                </StackPanel>
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModelsProject:SequenceViewModel}">
                <StackPanel>
                    <TextBlock Text="Displaying Sequence DataContract stuff..." />
                </StackPanel>
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModelsProject:GroupViewModel}">
                <StackPanel>


                    <DataGrid Margin="10" AutoGenerateColumns="False"
                                  Background="Transparent" BorderThickness="0"
                                  CanUserAddRows="False"
                                  CellStyle="{DynamicResource dgCellStyle}"
                                  ColumnHeaderStyle="{DynamicResource dgHeader}"
                                  DataContext="{Binding ElementName=SummaryTreeView,
                                                        Path=SelectedItem}"
                                  GridLinesVisibility="None"
                                  ItemsSource="{Binding Standards}"
                                  Padding="0" RowHeaderWidth="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                        <DataGrid.Resources>
                            <Style x:Key="dgHeader" TargetType="{x:Type DataGridColumnHeader}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                                            <TextBlock Width="{TemplateBinding Width}"
                                                           Padding="5"
                                                           Text="{TemplateBinding Content}"
                                                           TextAlignment="Left">
                                                    <TextBlock.Background>
                                                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                                            <GradientStop Offset="0.0" Color="#373638" />
                                                            <GradientStop Offset="1.0" Color="#77797B" />
                                                        </LinearGradientBrush>
                                                    </TextBlock.Background>
                                            </TextBlock>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="OverridesDefaultStyle" Value="True" />
                                <Setter Property="Background" Value="Green" />
                                <Setter Property="Foreground" Value="White" />
                                <Setter Property="FontSize" Value="12" />
                                <Setter Property="Background">
                                    <Setter.Value>
                                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                            <GradientStop Offset="0.0" Color="#373638" />
                                            <GradientStop Offset="1.0" Color="#77797B" />
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                            <Style x:Key="dgCellStyle" TargetType="{x:Type DataGridCell}">
                                <Style.Triggers>
                                    <Trigger Property="DataGridCell.IsSelected" Value="True">
                                        <Setter Property="Background" Value="White" />
                                        <Setter Property="Foreground" Value="Black" />
                                        <Setter Property="BorderThickness" Value="0" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </DataGrid.Resources>
                        <DataGrid.Columns>
                            <DataGridTemplateColumn Header="Standards">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>


                                        <Grid Margin="5">
                                            <Grid.RowDefinitions>
                                                <RowDefinition />
                                                <RowDefinition />
                                            </Grid.RowDefinitions>
                                            <TextBlock Grid.Row="0" Text="{Binding Path=StepMaster.Description}" VerticalAlignment="Center" FontWeight="SemiBold"/>
                                            <Grid Grid.Row="1">
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition />
                                                    <RowDefinition />
                                                </Grid.RowDefinitions>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="*" />
                                                </Grid.ColumnDefinitions>
                                                <TextBlock Grid.Row="0" Grid.Column="0" Text="Standard: " VerticalAlignment="Top"/>
                                                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Standard}" Margin="5,0,0,0" VerticalAlignment="Top" TextWrapping="Wrap"/>

                                                <TextBlock Grid.Row="1" Grid.Column="0" Text="Details: " VerticalAlignment="Top"/>
                                                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Details}" Margin="5,0,0,0" VerticalAlignment="Top" TextWrapping="Wrap"/>

                                                <TextBlock Grid.Row="2" Grid.Column="0" Text="Comments: " VerticalAlignment="Top"/>
                                                <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Comments}" Margin="5,0,0,0" VerticalAlignment="Top" TextWrapping="Wrap"/>
                                            </Grid>
                                        </Grid>

                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>


                </StackPanel>
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModelsProject:StepViewModel}">
                <UniformGrid Rows="3">
                    <TextBlock Margin="10" FontWeight="DemiBold"
                               Text="Standards" />
                    <TextBlock Margin="10" FontWeight="DemiBold"
                               Text="Standards Details" />
                    <StackPanel Orientation="Vertical">
                        <DataGrid Margin="10" AutoGenerateColumns="False"
                            <Another data grid is here />
                        </DataGrid>
                    </StackPanel>
                </UniformGrid>
            </DataTemplate>
        </ContentControl.Resources>
    </ContentControl>
</Border>

2 个答案:

答案 0 :(得分:3)

事实证明,使这项工作的唯一方法是直接在DataGrid中使用属性:

MaxWidth="{Binding ElementName=SummaryRightSideContainerBorder, Path=ActualWidth}" ColumnWidth="*" 

使用MaxWidth,以便Datagrid本身不超过其容器边界,并使用ColumnWidth,以便列使用数据网格中100%的可用空间。

Blam的回答让我找到了正确的解决方案。

答案 1 :(得分:1)

作为答案,您需要将第二列设置为

<ColumnDefinition Width="*"/>

因此受到限制。 *表示容器的大小。现在它正在容器(屏幕)之外发展

第一栏自动确定