编程语言
首页 > 编程语言> > C#-WPF简单表布局

C#-WPF简单表布局

作者:互联网

我正在为正在处理的WPF项目创建简单的布局.

我尝试过对Datagrid和GridView进行样式设置,但是它们都无法按我的方式工作,而且我不希望项目是可编辑的/可选择的,也不希望对列进行排序或类似的操作.基本上,我只想要一个没有钟声的简单动态表布局.

任何有关如何重新创建此建议将不胜感激.

更新:我需要基于ObservableCollection动态的行数

解决方法:

使用HeaderedItemsControl,XAML

    <!-- templates -->

    <DataTemplate x:Key="itemWithDeleteButton">
        <Grid Width="Auto">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="100"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="{Binding Path=DocumentName, Mode=OneWay}" />
            <Button Grid.Column="1" Command="{Binding DeleteCommand}"/>
        </Grid>
    </DataTemplate>

    <Style TargetType="{x:Type HeaderedItemsControl}" x:Key="DeletedGrid">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type HeaderedItemsControl}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="100"/>                              
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="20"/>
                            <RowDefinition Height="4*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Column="0" Grid.Row="0" Text="Document Name" 
                                   VerticalAlignment="Center"
                                   FontWeight="Bold"/>
                        <TextBlock Grid.Column="1" Grid.Row="0" Text="Actions"
                                   VerticalAlignment="Center"
                                   FontWeight="Bold"/>
                        <Grid Grid.Row="1" Grid.ColumnSpan="2" Width="Auto" Height="Auto" Background="White">
                            <ItemsPresenter/>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    <!-- control -->

    <HeaderedItemsControl Style="{StaticResource DeletedGrid}" Margin="0,0,0,10" 
                                  Grid.Row="4" Grid.ColumnSpan="2" ItemTemplate="{StaticResource itemWithDeleteButton}"                                      
                                  ItemsSource="{Binding GridData}">

视图模型

    public class GridItem
    {
      public string DocumentName { get; set; }
      public ICommand DeleteCommand { get; set; }
    }

    public class MyViewModel
    {
      public ObservableCollection<GridItem> GridData { get; set; }
    }

标签:wpf,c,net,caliburn-micro
来源: https://codeday.me/bug/20191031/1979059.html