如何从数据库对象值添加TextBlock文本值 [英] How to add TextBlock text value from database object value

查看:227
本文介绍了如何从数据库对象值添加TextBlock文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事WPF应用程序,其中有一个Window.xaml,我正在使用从数据库(MSSQL 2008R2)填充的dataGrid,在那个dataGrid中我正在从我的数据库加载订单。



我还通过numberOfOrder对订单进行分组,并且可以将其扩展或折叠,以防同时存在很多订单。



无论如何,我的扩展器是DockPanel,其中有TextBlock,我使用该textBlock来显示数据库中的每个顺序的数量,但我想知道如何从数据库中显示其他内容到该textBlock(for例如,当我将订单插入到数据库中时,我想显示DateTime - 我已经拥有该属性),让我们按照Order of Order的顺序排列所有内容,让我们像现在一样显示别的东西而不是numberOfOrder。



这是现在的样子的照片:





订单号:#1< ; - 我在TextBlock中显示,1是数据库中的numberOfOrder。

 < TextBlock FontWeight =NormalFontFamily =VerdanaFontSize =20Height =25Foreground =#83D744Text ={Binding Path = Name,StringFormat = Order of Order:#{0}}/> 

我想在这里做的是显示别的而不是订单号,就像我说的我想显示DateTime或许或者什么,所以我会看起来像这样:





这是完整的代码:



XAML:(TextBlock在这里很重要)

 < DataGrid.GroupStyle> 
<! - 顶级组的样式。 - >
< GroupStyle>
< GroupStyle.ContainerStyle>
< Style TargetType ={x:Type GroupItem}>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type GroupItem}>
< Expander IsExpanded =TrueBackground =BlackOpacity =0.7>
< Expander.Header>
< DockPanel Height =50Margin =0,0,0,0Name =dockPanelWidth ={Binding RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type DataGrid}} ,Path = ActualWidth}>

< Button> ...< / Button>
< Button> ...< / Button>

< TextBlock FontWeight =NormalFontFamily =VerdanaFontSize =20Height =25Foreground =#83D744Text ={Binding Path = Name,StringFormat = Number of订单:#{0}}/>

< / DockPanel>
< /Expander.Header>
< Expander.Content>
< ItemsPresenter />
< /Expander.Content>
< / Expander>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< /GroupStyle.ContainerStyle>
< / GroupStyle>
< /DataGrid.GroupStyle>

我用于填写DATAGRID的类别

  public class OrderLocal 
{
public string Title {get;组; }
public int数量{get;组; }
public int NumberOfOrder {get;组; }
public DateTime DateOfInput {get;组; }

}

我将填写DATAGRID的代码所有订单:

  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;


}


解决方案

p>也许你可以在DockPanel中放置另一个TextBlock?

 & DockPanel> 
< Button DockPanel.Dock =Right/>
< Button DockPanel.Dock =Right/>
< TextBlock Text =/>
< TextBlock文本无论你有什么/>
< TextBlock Text ={Binding Items,Converter = {StaticResource ItemsConverter},StringFormat = \ {0:d\}}/>
< / DockPanel>

和转换器:

 public class ItemsConverter:IValueConverter 
{
public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
{
ReadOnlyObservableCollection< object> items = value作为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();
}
}

就是这样。不同的转换,您可以从您的项目中获取不同的值。



关注



Steffen


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:

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:

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;


}

解决方案

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

这篇关于如何从数据库对象值添加TextBlock文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆