How to add TextBlock text value from database obje

2019-09-19 23:57发布

I'm working on WPF application which has one Window.xaml where I'm using dataGrid which is filled from Database (MSSQL 2008R2), In that dataGrid I'm loading orders from my Database.

I'm also grouping my orders by numberOfOrder, and its possible to expand them or to collapse them, in case there will be many orders at same time.

Anyway, inside of my expander Is DockPanel, which has textBlock there, I used that textBlock to display number of each order from database, but I am wondering how could I display something else from database into that textBlock (for example I want to display DateTime when I inserted my order into database - I've that property allready), let's keep everything grouped by Number of Order by lets display something else instead of numberOfOrder like I did until now.

Here is photo of how that looks right now:

enter image description here

Order Number: #1 <- I'm displaying this in TextBlock and 1 is numberOfOrder from my database.

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

And what I would like to do here is to display something else instead of Order Number, as I said I would like to display DateTime maybe or whatever, so I would make it look like this:

enter image description here

Here is full code:

XAML: (TextBlock 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 BEHIND WHERE I AM FILLING DATAGRID WILL 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;


}

1条回答
Rolldiameter
2楼-- · 2019-09-20 00:32

Maybe you can put just another TextBlock in 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();
    }
}

That's it. With different converted you can get different values from your items.

Regards

Steffen

查看更多
登录 后发表回答