How to add a TextBlock text value from a database object value

I am working on a WPF application that has one Window.xaml, where I use a dataGrid that is populated from a database (MSSQL 2008R2). In this dataGrid, I load orders from my database.

I also group my orders with numberOfOrder, and you can expand or collapse it if there are many orders at the same time.

Anyway, inside my expander there is a DockPanel that has a textBlock, I used this textBlock to display the number of each order from the database, but I wonder how I can display something else from the database in this text block (for example, I want to display DateTime when I inserted my order into the database - I have this property allready), let everything, grouped by the number of orders, allow me to display something else instead of numberOfOrder, as I did until now.

Here is a photo of what it looks like now:

enter image description here

Order number: # 1 <- I show this in a TextBlock, and 1 is the OffOrder number from my database.

<TextBlock FontWeight="Normal" FontFamily="Verdana" FontSize="20" Height="25" Foreground="#83D744" Text="{Binding Path=Name,StringFormat= Number of Order:# {0}}" /> 

And I would like to do here to display something else instead of Order Number, since I said that I would like to show DateTime, possibly anything, so I would do this:

enter image description here

Here is the complete code:

XAML: (TextBlock value is important here)

 <DataGrid.GroupStyle> <!-- Style for groups at top level. --> <GroupStyle> <GroupStyle.ContainerStyle> <Style TargetType="{x:Type GroupItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GroupItem}"> <Expander IsExpanded="True" Background="Black" Opacity="0.7"> <Expander.Header > <DockPanel Height="50" Margin="0,0,0,0" Name="dockPanel" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=ActualWidth}"> <Button>...</Button> <Button>...</Button> <TextBlock FontWeight="Normal" FontFamily="Verdana" FontSize="20" Height="25" Foreground="#83D744" Text="{Binding Path=Name,StringFormat= Number of Order:# {0}}" /> </DockPanel> </Expander.Header> <Expander.Content> <ItemsPresenter /> </Expander.Content> </Expander> </ControlTemplate> </Setter.Value> </Setter> </Style> </GroupStyle.ContainerStyle> </GroupStyle> </DataGrid.GroupStyle> 

MY CLASS WHICH IS USED TO FILL DATAGRID:

 public class OrderLocal { public string Title { get; set; } public int Quantity { get; set; } public int NumberOfOrder { get; set; } public DateTime DateOfInput { get; set; } } 

CODE FOR WHERE I FILL DATAGRID WILL BE ALL ORDERS:

 public MainWindow() { InitializeComponent(); this.WindowStartupLocation = WindowStartupLocation.CenterScreen; this.WindowState = WindowState.Maximized; var ordersList = OrdersController.localOrders(); collectionViewSource.Source = ordersList; collectionViewSource.GroupDescriptions.Add(new PropertyGroupDescription("NumberOfOrder")); DataContext = collectionViewSource; } 
-one
source share
1 answer

Maybe you can add another TextBlock to your DockPanel?

 <DockPanel> <Button DockPanel.Dock="Right"/> <Button DockPanel.Dock="Right"/> <TextBlock Text=""/> <TextBlock Text"whatever you wan"/> <TextBlock Text="{Binding Items, Converter={StaticResource ItemsConverter},StringFormat=\{0:d\}}"/> </DockPanel> 

and converter:

 public class ItemsConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { ReadOnlyObservableCollection<object> items = value as ReadOnlyObservableCollection<object>; if (items == null) throw new ArgumentNullException(nameof(items)); DateTime latestOrderDate = items.Max(x => x.OrderDate); return latestOrderDate; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

What is it. With the help of various transformations, you can get different values ​​from your elements.

Hi

Steffen

0
source

Source: https://habr.com/ru/post/1499654/


All Articles