none
datagird怎么让cell的内容过多的时候,能分行显示 RRS feed

  • 问题

  • 如图,列为datagridtextcolumn,width为窗体大小百分比,非auto,是否有wrap的类似属性或者风格模板?

    2012年3月26日 8:45

答案

  • 如下,指定DataGridTextColumn的ElementStyle:

      <Grid>
        <Grid.Resources>
          <Style  x:Key="gridTextColumnCellStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="TextWrapping" Value="Wrap"/>
          </Style>
        </Grid.Resources>
        <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
          <DataGrid.Columns>
            <DataGridTextColumn Width="30" Header="Name" Binding="{Binding Name}" ElementStyle="{StaticResource gridTextColumnCellStyle}"/>
          </DataGrid.Columns>
        </DataGrid>
      </Grid>


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 猪宝贝 2012年3月27日 11:43
    2012年3月27日 3:15
    版主

全部回复

  • 如下,指定DataGridTextColumn的ElementStyle:

      <Grid>
        <Grid.Resources>
          <Style  x:Key="gridTextColumnCellStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="TextWrapping" Value="Wrap"/>
          </Style>
        </Grid.Resources>
        <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
          <DataGrid.Columns>
            <DataGridTextColumn Width="30" Header="Name" Binding="{Binding Name}" ElementStyle="{StaticResource gridTextColumnCellStyle}"/>
          </DataGrid.Columns>
        </DataGrid>
      </Grid>


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 猪宝贝 2012年3月27日 11:43
    2012年3月27日 3:15
    版主
  • 也是没有效果,我的xaml如下:

    <DataGrid.Resources>
                            <Style TargetType="{x:Type TextBlock}" x:Key="gridTextColumnCellStyle">
                                <Setter Property="TextWrapping" Value="Wrap"/>
                            </Style>
    </DataGrid.Resources>

    <DataGrid.Columns>
                            <DataGridTextColumn Header="XXX" Width="10*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding fadevname}"/>
                            <DataGridTextColumn Header="XXX" Width="10*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding subsysid}"/>
                            <DataGridTextColumn Header="XXX" Width="5*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alatype}"/>
                            <DataGridTextColumn Header="XXX" Width="5*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alalevelid}"/>
                            <DataGridTextColumn Header="XXX" Width="10*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alatimefirst}"/>
                            <DataGridTextColumn Header="XXX" Width="10*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alatime}"/>
                            <DataGridTextColumn Header="XXX" Width="40*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alacontent}"/>
                            <DataGridTextColumn Header="XXX" Width="5*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding isackovertime}"/>
                            <DataGridTextColumn Header="XXX" Width="5*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding ischkovertime}"/>

    </DataGrid.Columns>

    2012年3月27日 4:43
  • 请给出你完整的XAML代码,你肯定有一些其他的设置。

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年3月27日 5:08
    版主
  • <ribbon:RibbonWindow x:Class="SPOSS2012.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
            Title="MainWindow"
      x:Name="RibbonWindow"
      Width="640" Height="480" WindowState="Maximized" WindowStartupLocation="CenterScreen" Loaded="RibbonWindow_Loaded" Unloaded="RibbonWindow_Unloaded">
       
        <Grid x:Name="LayoutRoot">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="57*" />
                <ColumnDefinition Width="561*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="294*" />
                <RowDefinition Height="10*"/>
            </Grid.RowDefinitions>

            <ribbon:Ribbon x:Name="Ribbon" Grid.ColumnSpan="2">
                <ribbon:Ribbon.ApplicationMenu>
                    <ribbon:RibbonApplicationMenu SmallImageSource="Images\SmallIcon.png">
                        <ribbon:RibbonApplicationMenuItem Header="Hello _Ribbon"
                                                          x:Name="MenuItem1"
                                                          ImageSource="Images\LargeIcon.png"/>
                    </ribbon:RibbonApplicationMenu>
                </ribbon:Ribbon.ApplicationMenu>
                <ribbon:RibbonTab x:Name="HomeTab"
                                  Header="Home">
                    <ribbon:RibbonGroup x:Name="Group1"
                                        Header="Group1">
                        <ribbon:RibbonButton x:Name="Button1"
                                             LargeImageSource="Images\LargeIcon.png"
                                             Label="ADS" Click="Button1_Click" />

                        <ribbon:RibbonButton x:Name="Button2"
                                             SmallImageSource="Images\SmallIcon.png"
                                             Label="Button2" />
                        <ribbon:RibbonButton x:Name="Button3"
                                             SmallImageSource="Images\SmallIcon.png"
                                             Label="Button3" />
                        <ribbon:RibbonButton x:Name="Button4"
                                             SmallImageSource="Images\SmallIcon.png"
                                             Label="Button4" />
                       
                    </ribbon:RibbonGroup>
                   
                </ribbon:RibbonTab>
            </ribbon:Ribbon>
            <StatusBar Grid.Row="2" HorizontalAlignment="Stretch" Name="statusBar1" VerticalAlignment="Stretch" Grid.ColumnSpan="2" />
            <TabControl Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch" Margin="0" Name="tabControl1" VerticalAlignment="Stretch" SelectedIndex="0">
                <TabItem Header="已接警" Foreground="#FF1CD81C">
                    <DataGrid ItemsSource="{Binding AckedTable}"  AutoGenerateColumns="False" Name="dataGrid2" LoadingRow="dataGrid2_LoadingRow" IsReadOnly="True">
                        <DataGrid.ColumnHeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                            </Style>
                        </DataGrid.ColumnHeaderStyle>
                       
              <DataGrid.Resources>
                             <Style TargetType="{x:Type TextBlock}" x:Key="gridTextColumnCellStyle">
                                 <Setter Property="TextWrapping" Value="Wrap"/>
                             </Style>
               </DataGrid.Resources>
               
                        <DataGrid.Columns>

                            <DataGridTextColumn Header="设备名称" Width="10*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding fadevname}"/>
                            <DataGridTextColumn Header="子系统名称" Width="10*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding subsysid}"/>
                            <DataGridTextColumn Header="告警类型" Width="5*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alatype}"/>
                            <DataGridTextColumn Header="告警等级" Width="5*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alalevelid}"/>
                            <DataGridTextColumn Header="第一次告警时间" Width="10*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alatimefirst}"/>
                            <DataGridTextColumn Header="告警时间" Width="10*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alatime}"/>
                            <DataGridTextColumn Header="告警内容" Width="40*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding alacontent}"/>
                            <DataGridTextColumn Header="接警超时" Width="5*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding isackovertime}"/>
                            <DataGridTextColumn Header="处警超时" Width="5*" ElementStyle="{StaticResource gridTextColumnCellStyle}" Binding="{Binding ischkovertime}"/>
     
                        </DataGrid.Columns>
                    </DataGrid>
                </TabItem>
            </TabControl>
            <!-- 侧边栏视图 -->
            <ListView Grid.Row="1" HorizontalAlignment="Stretch" Name="CAlarmListView" VerticalAlignment="Stretch" ItemsSource="{Binding}" HorizontalContentAlignment="Center" DataContext="{Binding}" Background="White" MouseLeftButtonDown="CAlarmListView_MouseLeftButtonDown">
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Vertical" HorizontalAlignment="Center"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.Resources>
                    <Style TargetType="ListViewItem">
                        <EventSetter Event="MouseLeftButtonDown" Handler="CAlarmListView_MouseLeftButtonDown"/>
                    </Style>
                </ListView.Resources>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Image Source="{Binding Pic}"/>
                            <TextBlock Text="{Binding Name}" Grid.Row="1"/>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </ribbon:RibbonWindow>

    以上完整的xaml,把datagrid嵌入在一个tabcontrol里,外层一个用来布局的grid以及ribbon

    2012年3月27日 6:07
  • 关帖子了? 请问你是否已经解决?  不好意思,你的 XAML没有问题,我这边是可以的 。

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us


    2012年3月27日 14:27
    版主