如何获取wpf datagrid中显示的行数

时间:2010-01-26 09:28:14

标签: wpf datagrid row

对于winform datagridview,我们可以使用“ DisplayedRowCount ”来获取显示行的编号,如何获取wpf datagrid。谢谢~~   芮

2 个答案:

答案 0 :(得分:0)

你会在这里找到答案;

http://xceed.com/CS/forums/thread/25456.aspx

将其传递给StaticResource

从那里你可以在对象上使用count方法。

答案 1 :(得分:0)

如果说要显示电影票列表,则可以获取这样的行数,并将其显示在datagrid选项卡中。请注意,在这里我没有使用WPF TabControl而是使用了GridStackPanels,因为TabControl在执行命令执行时遇到了一些困难:< / p>

在您的Resources.resx中,有一个像这样的项目:

  <data name="MovieTicketsDataGridTitle" xml:space="preserve">
    <value>Tickets ({0})</value>
  </data>

然后在您的XAML文件中具有:

        <!--  Table header  -->
        <Border Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" ">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <StackPanel  Orientation="Horizontal" VerticalAlignment="Top">
                    <Label Name="MovieTicketsDataGridTitle" 
                           Margin="0,0,20,0" 
                           Content="{Binding MovieTicketCount, ConverterParameter={x:Static resx:Resources.MovieTicketsDataGridTitle}, Converter={StaticResource DataGridHeaderCountConverter}}" 
                           MouseDown="MovieTicketsDataGridTitle_MouseDown">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseDown" >
                                <i:InvokeCommandAction Command="{Binding MovieTicketsHeaderMouseDown}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Label>
               </StackPanel >
                <Button Grid.Column="2" 
                        Width="30" 
                        Height="30" 
                        Margin="0,0,10,0" 
                        Background="Transparent" 
                        BorderThickness="0" 
                        HorizontalAlignment="Right" 
                        CommandParameter="{Binding ., Source={x:Reference MovieTicketsDataGrid}}"
                        Visibility="{Binding MovieTicketsGridVisibility}">
                    <StackPanel Orientation="Horizontal" Background="Transparent">
                        <Image Width="30" Height="30" Source="/Track Inspection;component/Icons/myTicketBtn.png" />
                    </StackPanel>
                </Button>
            </Grid>
        </Border>
       <controls:CustomDataGrid Name="MovieTicketsDataGrid" Grid.Row="1"
                                AutoGenerateColumns="False"
                                CanUserAddRows="False"
                                CanUserDeleteRows="False"
                                CanUserReorderColumns="False"
                                CanUserResizeColumns="False"
                                CanUserResizeRows="False"
                                HeadersVisibility="Column"
                                Visibility="{Binding MovieTicketGridVisibility}"
                                ItemsSource="{Binding MovieTicketCollectionView.View}"
                                NoDataText="{x:Static resx:Resources.DataGridNoData}"
                                RowHeight="60"
                                RowSelectionCommand="{Binding NavigateToMovieTicketDetails}"
                                VerticalGridLinesBrush="#3D9799">
            <controls:CustomDataGrid.Resources>
                <Style TargetType="DataGridColumnHeadersPresenter">
                    <Setter Property="Margin" Value="10,0,0,0" />
                </Style>
            </controls:CustomDataGrid.Resources>
            <DataGrid.Columns>

                <!--  Date  -->
                <DataGridTextColumn Width="*"
                                    Binding="{Binding MovieDate, StringFormat=d, NotifyOnTargetUpdated=True}"
                                    Header="{x:Static resx:Resources.MovieTicketsDataGridDate}"/>

                <!--  Theatre -->
                <DataGridTextColumn Width="*"
                                    Binding="{Binding Theatre},NotifyOnTargetUpdated=True}"
                                    Header="{x:Static resx:Resources.MovieTicketThreatre}" />

                <!--  Price -->
                <DataGridTextColumn Width="*"
                                    Binding="{Binding Price},NotifyOnTargetUpdated=True}"
                                    Header="{x:Static resx:Resources.MovieTicketPrice}" />

            </DataGrid.Columns>
        </controls:CustomDataGrid>

然后这就是我的视图模型中的内容:

    private Visibility _movieTicketsGridVisibility = Visibility.Visible;
    public Visibility MovieTicketsGridVisibility
    {
        get { return _movieTicketsGridVisibility; }
        set
        {
            _movieTicketsGridVisibility = value;
            NotifyPropertyChanged(nameof(MovieTicketsGridVisibility));
        }
    }

    private readonly object _movieTicketLock = new object();
    private FastObservableCollection<MovieTicket> _ticketCollection = new FastObservableCollection<MovieTicket>();

    public MovieTicketsViewModel() : base()
    {
        // ...
        MovieTicketsGridVisibility = Visibility.Visible;
        MovieTicketsCollectionView.Source = _ticketCollection;
        BindingOperations.EnableCollectionSynchronization(_ticketCollection, _movieTicketLock);
        // ...
        LoadMovieDetails();
    }

    private void LoadMovieDetails()
    {
        // ...
        MovieTicketCount = myTicketRepository.LoadMovieTicketCount();
        IList<MovieTicket> tickets =
            myTicketRepository.LoadMovieTickets();
        _ticketCollection.ReplaceAll(tickets);
        // ...
    }

这是必需的转换器:

public class DataGridHeaderCountConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string val = (value != null) ? value.ToString() : "0";
        string format = (parameter != null ? parameter.ToString() : "{0}");
        return (val != "0") ? format.Replace("{0}", val) : format.Replace("({0})", "");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}