2016-10-19 30 views
-1

我正在使用WPF應用程序,該應用程序有一個Window.xaml,我使用的是從數據庫(MSSQL 2008R2)填充的dataGrid,在那個dataGrid中我正在加載命令從我的數據庫。如何從數據庫對象值中添加TextBlock文本值

我還通過numberOfOrder對我的訂單進行了分組,並且可能將其展開或摺疊,以防在同一時間有多個訂單。

無論如何,在我的擴展器內部是DockPanel,它有textBlock那裏,我用那個textBlock顯示數據庫中的每個訂單的數量,但我想知道我怎麼能顯示其他數據庫到該textBlock(例如我當我將訂單插入數據庫時​​想顯示日期時間 - 我已經將該屬性設置爲已存在),讓我們按照訂單數量分組,讓所有按照NumberOfOrder顯示的其他內容顯示,就像我之前做過的那樣。

下面是如何看起來,現在的照片:

enter image description here

訂單號:#1 < - 我在TextBlock中顯示這一點,1是從我的數據庫numberOfOrder。

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

而我想在這裏做的是顯示的東西,而不是訂單號一樣,正如我所說的,我想也許或任何顯示日期時間,所以我想使它看起來像這樣:

enter image description here

這裏是全碼:

XAML:(TextBlock的是這裏重要的)

<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類,這是用來填充DATAGRID:

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

} 

後面的代碼我在哪裏灌裝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; 


} 

回答

0

也許你可以把又一個的TextBlock在你的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> 

和轉換器:

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(); 
    } 
} 

就是這樣。隨着不同的轉換,你可以從你的項目中獲得不同的值。

問候

斯特芬

相關問題