代码之家  ›  专栏  ›  技术社区  ›  Junior

如何将XAML视图中网格的最大宽度设置为最大对话框宽度?

  •  0
  • Junior  · 技术社区  · 6 年前

    我有一个WPF项目,它是使用C_在 prism框架的顶部编写的。

    当按下I use InteractionRequest 以显示对话框时,我有一个按钮。此对话框的视图宽度为550。

    视图是用网格设计的,我希望网格的宽度也为550,这是对话框的最大宽度。

    然而,当列中的文本很长时,网格似乎总是比对话框延伸更多。

    下面是一个屏幕截图,可以更好地解释视觉问题

    图片中的第一行表示一个 title where the second line representative that description

    这是呈现此对话框/视图的XAML代码

    <usercontrol x:class=“modules.register.views.RecalltTransactionView”
    xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation”(http://schemas.microsoft.com/winfx/2006/xaml/presentation)
    xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”
    xmlns:mc=“http://schemas.openxmlformats.org/markup-compatibility/2006”
    xmlns:d=“http://schemas.microsoft.com/expression/blend/2008”
    xmlns:local=“clr-namespace:modules.register.views”
    xmlns:fa=“http://schemas.fontawesome.io/icons/”
    xmlns:materialdesign=“http://materialdesigninxalm.net/winfx/xaml/themes”
    xmlns:prism=“http://prismlibrary.com/”
    prism:viewModelLocator.autowireviewModel=“真”
    textfelement.foreground=“动态源材料设计主体”
    textfelement.fontweight=“常规”
    textfelement.fontsize=“14”
    textOptions.textformatingMode=“理想”
    textOptions.textRenderingMode=“自动”
    mc:ignorable=“d”
    宽度=“550”& gt;
    
    <grid width=“550”>
    
    <datagrid itemssource=“绑定对话框.savedTransactions”
    autoGenerateColumns=“假”
    isreadOnly=“真”
    HorizontalSignment=“伸展”
    VerticalAlignment=“顶部”
    canUserAddRows=“假”
    VerticalContentAlignment=“中心”
    HorizontalContentAlignment=“拉伸”
    cellstyle=“静态资源中心数据报单元格”>
    
    <datagrid.columns>
    
    <DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
    <数据模板>
    <堆叠面板>
    <textBlock text=“绑定标题”textWrapping=“wrap”/>gt;
    <textBlock text=“绑定说明”textwrapping=“wrap”/>gt;
    </stackpanel>
    </datatemplate>
    </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    
    <DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
    <数据模板>
    <button verticalAlignment=“中心”
    command=“绑定relativesource=relativesource ancestortype=x:类型datagrid,
    path=dataContext.selectTransaction”
    commandParameter=“绑定”>
    
    <stackpanel orientation=“水平”>
    <fa:fontAwesome icon=“眼睛”
    fontsize=“18”/>
    
    <textBlock text=“调用”
    padding=“7 0 0 0”/>
    </stackpanel>
    </button>
    </datatemplate>
    </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    
    </datagrid.columns>
    </datagrid>
    
    和/格栅& GT;
    </usercontrol>
    < /代码> 
    
    

    正如您所看到的,两者都是titleanddescriptionhavetextfwrapping=“wrap p”but for some reason it is not wrapping.

    我基本上是想删除水平滚动条,但强制文本换行。如何解决此问题?[答:].

    当我按下一个我用的按钮InteractionRequest显示对话框。此对话框的视图宽度为550。

    视图是用网格设计的,我希望网格的宽度为550,这也是对话框的最大宽度。

    然而,当列中的文本很长时,网格似乎总是比对话框延伸更多。

    下面是一个屏幕截图,可以更好地解释视觉问题

    enter image description here

    图中的第一行表示title第二行代表description

    这是呈现此对话框/视图的XAML代码

    <UserControl x:Class="Modules.Register.Views.RecallTransactionView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:Modules.Register.Views"
                 xmlns:fa="http://schemas.fontawesome.io/icons/"
                 xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
                 xmlns:prism="http://prismlibrary.com/"
                 prism:ViewModelLocator.AutoWireViewModel="True"
                 TextElement.Foreground="{DynamicResource MaterialDesignBody}"
                 TextElement.FontWeight="Regular"
                 TextElement.FontSize="14"
                 TextOptions.TextFormattingMode="Ideal"
                 TextOptions.TextRenderingMode="Auto"
                 mc:Ignorable="d" 
                 Width="550">
    
        <Grid Width="550" >
    
            <DataGrid ItemsSource="{Binding Dialog.SavedTransactions}"
                      AutoGenerateColumns="False"
                      IsReadOnly="True"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Top"
                      CanUserAddRows="False"
                      VerticalContentAlignment="Center"
                      HorizontalContentAlignment="Stretch"
                      CellStyle="{StaticResource CenterDataGridCell}">
    
                <DataGrid.Columns>
    
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Title}" TextWrapping="Wrap" />
                                    <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button VerticalAlignment="Center"
                                        Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, 
                                                          Path=DataContext.SelectTransaction}"
                                        CommandParameter="{Binding}">
    
                                    <StackPanel Orientation="Horizontal">
                                        <fa:FontAwesome Icon="Eye"
                                                        FontSize="18" />
    
                                        <TextBlock Text="Recall"
                                                   Padding="7 0 0 0" />
                                    </StackPanel>
                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                </DataGrid.Columns>
            </DataGrid>
    
        </Grid>
    </UserControl>
    

    如你所见TitleDescriptionTextWrapping="Wrap"但由于某种原因,它没有包装。

    我基本上是想删除水平滚动条,但强制文本换行。如何解决此问题?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Cinchoo    6 年前

    您需要将第一个网格列的宽度设置为“*”

                <DataGridTemplateColumn Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Title}" TextWrapping="Wrap" />
                                <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>