How to Get the number of the displayed row in wpf datagrid
For winform datagridview , we can use "DisplayedRowCount" to get the number of the displayed row, How can i get it for wpf datagrid. Thank you开发者_开发百科~~ Rui
You'll find the answer here;
http://xceed.com/CS/forums/thread/25456.aspx
Pass it to a StaticResource
from there you can use the count method on the object.
You could get the number of rows like this, and display it in the datagrid tab, if say you were displaying a list of movie tickets. Please note that here instead of using a WPF TabControl
I ended up using a Grid
and StackPanels
because of some difficulty I had with the TabControl
in working with command execution:
In your Resources.resx, have an item like this:
<data name="MovieTicketsDataGridTitle" xml:space="preserve">
<value>Tickets ({0})</value>
</data>
Then in your XAML file have:
<!-- 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>
Then this is what I have in my view model:
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);
// ...
}
Here is the required converter:
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();
}
}
精彩评论