The use of the WPF DataGrid control uses the column template to beautify the data format

   <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.1*" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Button  Content="刷新"
                         FontSize="25"
                         Command="{Binding ExecuteRefreshCommand}" />
                <DataGrid ItemsSource="{Binding deviceInfos}"
                          VerticalAlignment="Stretch"
                          Grid.Row="1"
                          Margin="10,20,10,0"
                          RowDetailsVisibilityMode="Collapsed"
                          GridLinesVisibility="None"
                          IsReadOnly="True"
                          AllowDrop="True"
                          FontSize="25"
                          Cursor="Hand"
                          ColumnWidth="*"
                          AutoGenerateColumns="False"
                          CanUserReorderColumns="False"
                          CanUserResizeColumns="False"
                          CanUserSortColumns="False"
                          IsSynchronizedWithCurrentItem="False"
                          SelectionUnit="FullRow"
                          SelectionMode="Single"
                          SelectedIndex="{Binding deviceIndex}"
                          Style="{StaticResource RubyerDataGrid}"
                          Foreground="White">
 
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="设备名称"
                                            HeaderStyle="{StaticResource Amcz}"
                                            ElementStyle="{StaticResource contentCenterStyle}"
                                            Binding="{Binding name}" />
 
                        <DataGridTextColumn Header="设备IP地址"
                                            HeaderStyle="{StaticResource Amcz}"
                                            ElementStyle="{StaticResource contentCenterStyle}"
                                            Binding="{Binding ipaddress}" />
                        <DataGridTemplateColumn    Header="设备状态"
                                                   HeaderStyle="{StaticResource Amcz}">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding isOnLine,Converter={StaticResource bootToStringOnline}}"
                                               Foreground="{Binding isOnLine,Converter={StaticResource boolTobrushOnline} }"
                                               FontWeight="Bold"
                                               HorizontalAlignment="Center"
                                               VerticalAlignment="Center" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
 
                        <DataGridTemplateColumn Header="操作"
                                                HeaderStyle="{StaticResource Amcz}">
 
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate  >
                                    <StackPanel Orientation="Horizontal"
                                                VerticalAlignment="Center"
                                                HorizontalAlignment="Center"
                                                Margin="5"
                                              >
                                        <Button Height="auto"
                                                Width="auto"
                                              
                                                FontSize="20"
                                                HorizontalContentAlignment="Center"
                                                VerticalContentAlignment="Center"
                                                Margin="0,0,15,0"
                                                IsEnabled="{Binding isOnLine}"
                                                Command="{Binding DataContext.ExecuteCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}">
                                            <StackPanel  Orientation="Horizontal"
                                                         VerticalAlignment="Center"
                                                         HorizontalAlignment="Center">
                                                <Label HorizontalAlignment="Left"
                                                       VerticalAlignment="Center"
                                                       Content="{materialdesign:PackIcon Pencil, Size=25}"
                                                       Margin="0,0,5,0" />
                                                <TextBlock Text="查看"
                                                           HorizontalAlignment="Left"
                                                           VerticalAlignment="Center" />
                                            </StackPanel>
                                        </Button>
 
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
 
                    </DataGrid.Columns>
 
                    <DataGrid.ColumnHeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
 
                            <Setter Property="Background"
                                    Value="{DynamicResource PrimaryHueMidBrush}">
                            </Setter>
 
                        </Style>
                    </DataGrid.ColumnHeaderStyle>
 
                    <DataGrid.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="BorderBrush"
                                    Value="{DynamicResource PrimaryHueMidBrush}">
                            </Setter>
                            <Setter Property="Background"
                                    Value="#003366">
                            </Setter>
                            <Setter Property="Foreground"
                                    Value="White">
                            </Setter>
                        </Style>
                    </DataGrid.CellStyle>
                </DataGrid>
            </Grid>

Converter: convert bool type to Brush type

 public class BoolToBrushOnLineConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                if (value is bool)
                {
                    if ((bool)value)
                    {
                        return new SolidColorBrush(Colors.Green);
                    }
                    else
                    {
                        return new SolidColorBrush(Colors.Red);
                    }
                }
            }
            return new SolidColorBrush(Colors.White);
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Guess you like

Origin blog.csdn.net/zhang8593/article/details/129420134