wpf笔记

button动态弹出ContextMenu上下文菜单

http://www.cnblogs.com/lyghost/archive/2012/11/02/2751584.html

http://wenwen.soso.com/z/q221186332.htm

http://blog.sina.com.cn/s/blog_3e653ee60100ne19.html

http://www.cnblogs.com/chengxingliang/archive/2011/02/14/1954399.html


-------------------------------------------------------------------------------------------------------------------------------------

环境变量: 一边程序在下次启动的时候 可以读取上次启动的时候的配置项。(这样就可以避免使用ini,xml的配置文件了)

IDictionary environment = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable("PMB_CONNSTRS", conns, EnvironmentVariableTarget.Machine);

-------------------------------------------------------------------------------------------------------------------------------------


listview绑定数据源对象 与数据源更新同步

http://zhidao.baidu.com/question/124595286.html

listview

ComboBox Editing, Sorting, Grouping ListView

http://www.codeproject.com/Articles/29516/ComboBox-Editing-Sorting-Grouping-ListView


下面这套组件 要收费,不过可以对xlsx 进行导入到一个空间中并显示等等功能:ComponentOne Studio for WPF。

EPPlus这个工具是免费的,可以对xlsx进行读取,不需要额外的组件。


--------------------------------------------------------------listview动态绑定列,添加,删除行,滚动到最后一行

http://zhidao.baidu.com/question/124595286.html

http://www.cnblogs.com/whalechen/archive/2010/01/25/1655685.html

http://tech.ddvip.com/2009-02/1234417341108422.html

<Window x:Class="XLSX_Report.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStartupLocation="CenterScreen"
    Title="XLSX_Report" Width="800" Height="850"
    xmlns:lx="clr-namespace:XLSX_Report">
    <!--<Window.Resources>
        <lx:xlsx x:Key="xlsx"/>
    </Window.Resources>-->

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="700"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>
        <ListView Grid.Row="0" Name="BscAndMscListView"  IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" >
            <ListView.View>
                <GridView AllowsColumnReorder="True">
                    <GridViewColumn Header="Operate">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Delete" Tag="{Binding}" Click="Btn_Delete_Click" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Width="200"  Content="Add some" Click="Btn_Add_Click"  HorizontalAlignment="Center"/>
            <Button Width="200"  Content="Apply" Click="Btn_Add_Click"  HorizontalAlignment="Center"/>
        </StackPanel>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.IO;
using OfficeOpenXml;


namespace XLSX_Report
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        static DataTable resultTable = null;
        
        public Window1()
        {
            InitializeComponent();
            GridView tempGridView = (GridView)this.BscAndMscListView.View;
            //xlsx xls = (xlsx)this.Resources["xlsx"];
            FileStream ffss = new FileStream("POI类型--属性对应配置表_For830.xlsx", FileMode.Open);
            resultTable = xlsx.ReadByExcelLibrary(ffss);
            ffss.Close();

            //删除第一行文字标题列
            //resultTable.Rows.RemoveAt(0);
            for (int i = 0; i < resultTable.Rows.Count; )
            {
                DataRow dr = resultTable.Rows[i];
                string s = Convert.ToString(dr[5]).Trim();
                if (s.Equals(""))
                {
                    resultTable.Rows.RemoveAt(i);
                }
                else
                {
                    i++;
                }
            }
            for (int i = 0; i < resultTable.Columns.Count; i++ )
            {
                //这些不显示
                if (i>0 && i != 5 && i < 12) continue;
                DataColumn col = resultTable.Columns[i];
                /******************************************/
                GridViewColumn column = new GridViewColumn();
                GridViewColumnHeader h = new GridViewColumnHeader();
                h.Content = col.ColumnName;
                h.Tag = "*******";
                column.Header = h;
                DataTemplate dt = new DataTemplate();
                FrameworkElementFactory fef = new FrameworkElementFactory(typeof(TextBox));
                Binding binding = new Binding();
                binding.Path = new PropertyPath(col.ColumnName);
                fef.SetBinding(TextBox.TextProperty, binding);
                fef.SetValue(TextBox.BackgroundProperty, Brushes.AliceBlue);
                //fef.SetValue(TextBox.WidthProperty,20.0);
                dt.VisualTree = fef;
                column.CellTemplate = dt;
                tempGridView.Columns.Add(column);
                /******************************************/
            }

            this.BscAndMscListView.DataContext = resultTable;
        }
        private void Btn_Delete_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (sender as Button).DataContext as DataRowView;
            DataRow dr = drv.Row;
            resultTable.Rows.Remove(dr);
        }
        private void Btn_Add_Click(object sender, RoutedEventArgs e)
        {
            DataRow dr = resultTable.NewRow();
            resultTable.Rows.Add();
            int lastLine = BscAndMscListView.Items.Count - 1;
            //跑到最后一行中
            BscAndMscListView.ScrollIntoView(BscAndMscListView.Items[lastLine]);
            //设定选中行
            BscAndMscListView.SelectedItem = BscAndMscListView.Items[lastLine];
        }


    }

    public class xlsx : DataTable//System.Collections.ObjectModel.ObservableCollection<DataRow>
    {
        public static DataTable ReadByExcelLibrary(Stream xlsStream)
        {
            DataTable table = new DataTable();
            using (ExcelPackage package = new ExcelPackage(xlsStream))
            {
                //ExcelWorksheet sheet = package.Workbook.Worksheets[1];
                
                ExcelWorksheet sheet=null;

                foreach (ExcelWorksheet sht in package.Workbook.Worksheets)
                {
                    string sheetname = sht.Name.Trim();
                    if (sheetname.Equals("POI类型-属性配置表", StringComparison.CurrentCultureIgnoreCase))
                    {
                        sheet = sht;
                        break;
                    }
                }                


                int colCount = sheet.Dimension.End.Column;
                int rowCount = sheet.Dimension.End.Row;
                for (ushort j = 1; j <= colCount; j++)
                {
                    if (sheet.Cells[1, j].Value != null)
                    {
                        table.Columns.Add(new DataColumn(sheet.Cells[1, j].Value.ToString()));
                    }
                    else 
                    {
                        table.Columns.Add("");
                    }
                    
                }
                for (ushort i = 1; i <= rowCount; i++)
                {
                    DataRow row = table.NewRow();
                    for (ushort j = 1; j <= colCount; j++)
                    {
                        if (sheet.Cells[i, j].Value != null)
                        {
                            row[j - 1] = sheet.Cells[i, j].Value;
                        }
                        else 
                        {
                            row[j - 1] = "";
                        }
                    }
                    table.Rows.Add(row);
                }
            }
            return table;
        }
    }
}



----------------------------------绑定IsEnabled属性

<DockPanel>
            <RadioButton Name="rbtn1" IsChecked="True" GroupName="group1"></RadioButton>
            <TextBox Height="50" IsEnabled="{Binding ElementName=rbtn1, Path=IsChecked}" 
                     Background="AliceBlue" AcceptsReturn="True">输入XLSX全路径</TextBox>
        </DockPanel>

posted @ 2012-12-17 11:54  bielidefeng  阅读(191)  评论(0编辑  收藏  举报