`

work_代码添加

 
阅读更多
<UserControl x:Class="WpfCtrlLib.Ui.QuotesCtrl.QuotesCard"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"
             >

    <Grid Name="gQuotesCard" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
        <Grid.Resources>
            <Style TargetType="DataGridCell" x:Key="cellStyle">
                <Setter Property="Foreground" Value="White"/>
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <DataGrid Grid.Row="0" x:Name="gQuotesCardList" CanUserSortColumns="True" FrozenColumnCount="1" Margin="2">
        </DataGrid>
    </Grid>
    
</UserControl>



using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using WpfCtrlLib.Model.Quotes;
using WpfCtrlLib.Pub;
using WpfCtrlLib.Service.Quotes;
using WpfCtrlLib.Service.Quotes.Imp;
using WpfCtrlLib.Ui.UserCtrl;
using WpfCtrlLib.Util;
namespace WpfCtrlLib.Ui.QuotesCtrl
{
    /// <summary>
    /// QuotesCard.xaml 的交互逻辑
    /// </summary>
    public partial class QuotesCard : UserControl
    {
        private IQuotesPubService qcs = new QuotesPubServiceImpl();
       
        public QuotesCard()
        {
            InitializeComponent();

            DataGrid dg = new DataGrid();
            List<Colums> list = qcs.getColumns();
            double totalWidth = 1000;
            Window win = Mgr.CurrentWindow;
            if (win.Name == "quotes")
            {
                WinQuotes quotes = (WinQuotes)win;
                QuotesPanel qp = quotes.qPanel;
                QuotesContent qc = qp.Price;
                qc.UpdateLayout();
                //totalWidth = qc.ActualWidth;
            }
            else if (win.Name == "winTradeAndQuotes")
            {
                WinTradeAndQuotes tradeAndQuotes = (WinTradeAndQuotes)win;
                QuotesPanel qp = tradeAndQuotes.quotesPanel;
                QuotesContent qc = qp.Price;
                qc.UpdateLayout();
                //totalWidth = qc.ActualWidth;
            }
            totalWidth = totalWidth - 9;
            this.gQuotesCard.Width = totalWidth;
            double width = totalWidth / list.Count;
            //
            for (int i = 0; i < list.Count; i++)
            {
                DataGridTextColumn dtc = new DataGridTextColumn();
                dtc.Header = StringUtil.I18N(list[i].Name);
                Binding bind = new Binding(list[i].Name);
                //PriceDataConverter con=new PriceDataConverter();
                //bind.Converter = con;
                dtc.Binding = bind;
                dtc.Width = width;

                dtc.IsReadOnly = true;
                Style style = new Style(typeof(DataGridCell));
                Setter set = new Setter(DataGridCell.ForegroundProperty, Brushes.Red);
                Setter set1 = new Setter(DataGridCell.BackgroundProperty, Brushes.Black);
                style.Setters.Add(set);
                dtc.CellStyle = style;
                Style headerStyle = new Style(typeof(DataGridColumnHeader));
                Setter headerSet = new Setter(DataGridCell.ForegroundProperty, Brushes.DarkGray);
                headerStyle.Setters.Add(headerSet);
                headerStyle.Setters.Add(set1);
                dtc.HeaderStyle = headerStyle;

                dg.Columns.Add(dtc);
            }
            dg.Background = Brushes.Black;
            this.gQuotesCard.Children.Add(dg);
        }

        public QuotesCard(List<FQContract> contracts)
        {
            InitializeComponent();
            try
            {
                //DataGrid dg = new DataGrid();
                DataGrid dg = this.gQuotesCardList;
                List<Colums> list = qcs.getColumns();

                //this.gQuotesCard.UpdateLayout();
                //double w = this.gQuotesCard.ActualWidth;;

                double totalWidth = 1000;
               // Window win = Mgr.CurrentWindow;
                if (Mgr.WinQuote!=null) {
                    WinQuotes quotes = (WinQuotes)Mgr.WinQuote;
                    QuotesPanel qp = quotes.qPanel;
                    QuotesContent qc = qp.Price;
                    qc.UpdateLayout();
                    //totalWidth = qc.ActualWidth;
                }
                else if (Mgr.WinTradeAndQuote!=null)
                {
                    WinTradeAndQuotes tradeAndQuotes = (WinTradeAndQuotes)Mgr.WinTradeAndQuote;
                    QuotesPanel qp = tradeAndQuotes.quotesPanel;
                    QuotesContent qc = qp.Price;
                    qc.UpdateLayout();
                    //totalWidth = qc.ActualWidth;
                }
               // totalWidth = totalWidth;
                //this.gQuotesCard.Width = totalWidth;
                //this.gQuotesCard.UpdateLayout();
                //double width = totalWidth/ list.Count;
                //
                for (int i = 0; i < list.Count; i++)
                {

                    DataGridTextColumn dtc = new DataGridTextColumn();
                    dtc.Header = StringUtil.I18N(list[i].Name);
                    Binding bind = new Binding(list[i].Name);
                    if (list[i].Name == "NewPrice" || list[i].Name == "OpenPrice" || list[i].Name == "HighPrice" || list[i].Name == "ClosePrice" || list[i].Name == "BuyPrice" || list[i].Name == "SellPrice" || list[i].Name == "AvgPrice" || list[i].Name == "LowPrice")
                    {
                        PriceDataConverter priceConverter=new PriceDataConverter();
                        bind.Converter = priceConverter;
                    }
                    //dtc.Foreground = Brushes.White;
                    dtc.Binding = bind;
                    //dtc.Width = new DataGridLength();
                    dtc.IsReadOnly = true;

                    Style style = new Style(typeof(DataGridCell));
                    Setter set = new Setter(DataGridCell.ForegroundProperty, Brushes.Red);
                   Setter set1 = new Setter(DataGridCell.BackgroundProperty,Brushes.Black);
                    style.Setters.Add(set);
                    dtc.CellStyle = style;
                    Style headerStyle = new Style(typeof(DataGridColumnHeader));
                    Setter headerSet = new Setter(DataGridCell.ForegroundProperty, Brushes.DarkGray);
                    headerStyle.Setters.Add(headerSet);
                    headerStyle.Setters.Add(set1);
                    dtc.HeaderStyle = headerStyle;
                    //设置宽度
                    //DataGridLength.SizeToCells.;
                    //dtc.Width = DataGridLength.Auto;
                    dg.GridLinesVisibility = DataGridGridLinesVisibility.None;//设置没有边框
                    dg.Columns.Add(dtc);
                    
                }
                //获取合同信息
                ObservableCollection<QuotesPlateModel> oc = new ObservableCollection<QuotesPlateModel>();

                List<QuotesPlateModel> contractsList = qcs.getPalteContractsInfo(contracts);
                for (int i = 0; i < contractsList.Count; i++)
                {
                   oc.Add(contractsList[i]);
                }
                //设置行样式
                Style rowStyle=new Style(typeof(DataGridRow));
                Trigger rowTrigger = new Trigger();//触发器
                rowTrigger.Property = DataGridRow.IsMouseOverProperty;
                rowTrigger.Value = true;
                Setter trggerSet = new Setter(DataGridRow.BackgroundProperty, Brushes.Gold);
                Setter rowSet = new Setter(DataGridRow.BackgroundProperty, Brushes.Black);
                rowTrigger.Setters.Add(trggerSet);
                rowStyle.Setters.Add(rowSet);
                rowStyle.Triggers.Add(rowTrigger);
                dg.RowStyle=rowStyle;

                dg.CanUserAddRows = false;
                dg.AutoGenerateColumns = false;
                dg.HeadersVisibility = DataGridHeadersVisibility.Column;
                dg.ItemsSource = oc;


                /*DataGrid添加行双击击事件*/
                dg.MouseDoubleClick += (object sender, MouseButtonEventArgs e) =>
                {
                    Point aP = e.GetPosition(dg);
                    IInputElement obj = dg.InputHitTest(aP);
                    DependencyObject target = obj as DependencyObject;


                    while (target != null)
                    {
                        if (target is DataGridRow)
                        {
                            break;
                        }
                        target = VisualTreeHelper.GetParent(target);
                    }

                    DataGridRow row = target as DataGridRow;
                    if (row == null || row.DataContext == null) return;
                    QuotesPlateModel model = row.DataContext as QuotesPlateModel;

                    if (model == null) return;
                   
                    Mgr.FlashService.Show(model.ContractID);

                };
                //添加选中事件
               // dg.MouseDown+=dg_MouseDown;
                dg.Background = Brushes.Black;
                dg.SelectionChanged+=new SelectionChangedEventHandler(dg_SelectionChanged);
                //this.gQuotesCard.Children.Add(dg);
            }
            catch (AppException app)
            {
                MessageBox.Show(app.Message, StringUtil.I18N("sys_info"), MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                string errString = ex.Message;
                Console.WriteLine("=================errString:" + errString);
                MessageBox.Show(errString);
                //MessageBox.Show(StringUtil.I18N("sys_operation_failed"), StringUtil.I18N("sys_info"), MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
        /// <summary>
        /// 选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                DataGrid dg = (DataGrid)sender;
                var items = dg.SelectedItems;
                if (items.Count > 0) {
                    QuotesPlateModel model = (QuotesPlateModel)items[0];
                    //qcs.updatePriceStatistics(model);
                }
            }
            catch (AppException app)
            {
                MessageBox.Show(app.Message, StringUtil.I18N("sys_info"), MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                string errString = ex.Message;
                Console.WriteLine("=================errString:" + errString);
                MessageBox.Show(StringUtil.I18N("sys_operation_failed"), StringUtil.I18N("sys_info"), MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
        

        
    }
    //控制表格价格的显示
    [ValueConversion(typeof(string), typeof(string))]
    public class PriceDataConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string result = "--";
            string v = value.ToString();
            if (v == "--")
            {
                return result;
            }
            result = StringUtil.GetPriceData(v, true);
            //result = StringUtil.GetPrice(v);
            return result;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics