Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

c# - How to add TextBlock text value from database object value

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;


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...