为什么只有在指定TexBlock宽度时,Grid中TextBlock的TextWrapping才有效?

时间:2016-12-09 11:21:03

标签: wpf xaml

为什么只有在指定TexBlock宽度时,Grid中TextBlock的TextWrapping才有效?

当我删除TextBlock时MaxWidth属性TextWrapping无效。

ComboBox的完整代码

<Window Height="600" Width="480">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="4*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <ComboBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding MyList}" SelectedIndex="0" x:Name="comboBox" HorizontalAlignment="Stretch" VerticalAlignment="Top">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Margin="2" Text="{Binding Code}"></TextBlock>
                        <TextBlock Margin="2" Text="{Binding Name}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid ShowGridLines="False">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition MaxWidth="50"/>
                                        <ColumnDefinition Name="Column1"  MaxWidth="300"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0" Text="{Binding Code}"/>
                                    <TextBlock Grid.Column="1" MaxWidth="{Binding ElementName=Column1, Path=MaxWidth}" Text="{Binding Name}" TextWrapping="Wrap"/>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>
    </Grid>
</Window>

2 个答案:

答案 0 :(得分:2)

原因是将ColumnDefinition定义为Auto*not setting it at all,您无法限制TextBlock可用的大小。因此,文本的预期行为不会被包裹。因此,您必须直接在ColumnDefinition或TextBlock上定义Width或MaxWidth,以便包装工作。

答案 1 :(得分:1)

  

为什么只有在指定TexBlock宽度时,Grid中TextBlock的TextWrapping才有效?

因为在打开ComboBox时,Grid会被添加到不可见的ScrollViewer中。您可以通过简单地将ComboBox的ScrollViewer.Horizo​​ntalScrollBarVisibility附加属性设置为Disabled来解决此问题并将文本换行:

<ComboBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding MyList}"
              SelectedIndex="0" x:Name="comboBox" HorizontalAlignment="Stretch"
              VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="2" Text="{Binding Code}"></TextBlock>
                    <TextBlock Margin="2" Text="{Binding Name}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
        <ComboBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Width="100" />
            </ItemsPanelTemplate>
        </ComboBox.ItemsPanel>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="ComboBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid ShowGridLines="False">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MaxWidth="50"/>
                                    <ColumnDefinition MaxWidth="300"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="Code..."/>
                                <TextBlock Grid.Column="1"
                                           Text="some long text some long text some long text some long text some long text" 
                                           TextWrapping="Wrap"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>
  </ComboBox>