/// <summary> /// Extension method to convert a ICollectionView to a List /// </summary> public static IList <T> ViewToList <T>(this System.ComponentModel.ICollectionView view) { IList <T> resList = new List <T>(); var lr = view as System.Windows.Data.ListCollectionView; if (view.Groups != null) { foreach (var group in view.Groups) { System.Windows.Data.CollectionViewGroup cvGroup = group as System.Windows.Data.CollectionViewGroup; // Group(Name) would be: cvGroup.Name.ToString() foreach (var item in cvGroup.Items) { resList.Add((T)item); } } } else { foreach (var item in view.SourceCollection) { resList.Add((T)item); } } return(resList); }
/// <summary> /// Check which view mode is selected and update the current view /// </summary> /// <param name="aViewMode"></param> private void UpdatePropertyView(ViewModes aViewMode) { // Get the view System.ComponentModel.ICollectionView crtView = CollectionViewSource.GetDefaultView(mProperties); if (crtView == null) { return; } if (aViewMode == ViewModes.Alphabetical) { // Remove the groups crtView.GroupDescriptions.Clear(); // Sort options alphabetically by name crtView.SortDescriptions.Add(new SortDescription("DisplayName", ListSortDirection.Ascending)); } else if (aViewMode == ViewModes.Grouped) { // Remove the sorting crtView.SortDescriptions.Clear(); // Add the options groups crtView.GroupDescriptions.Add(new PropertyGroupDescription("Category")); } crtView.Refresh(); }
private void HandleFilterThrottle(string text) { System.ComponentModel.ICollectionView collectionView = System.Windows.Data.CollectionViewSource.GetDefaultView(ItemsSource); if (collectionView == null) { return; } collectionView.Filter = (item) => FilterPredicate(item, text); }
public MainWindow() { InitializeComponent(); this.appState = new AppState(); allBrowserTabs = new ObservableCollection <BrowserTab>(); AddInfo("Hello from ReloadIt :)"); this.mainGrid.DataContext = appState; this.tabsCv = System.Windows.Data.CollectionViewSource.GetDefaultView(allBrowserTabs); this.listView1.ItemsSource = tabsCv; this.dockPanel2.Visibility = Visibility.Collapsed; this.lvMessages.ItemsSource = this.appState.MessagesCv; this.rememberedPathErrors = new Dictionary <String, String>(); versionString = String.Format("v{0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()); this.Title = "ReloadIt " + versionString; }
/// <summary> /// Apply the new filter property to the current view and option list /// </summary> private void FilterPropertyView() { // Get the view System.ComponentModel.ICollectionView crtView = CollectionViewSource.GetDefaultView(mProperties); if (crtView == null) { return; } // if the SearchBox is not empty apply the filter if (false == string.IsNullOrWhiteSpace(SearchText)) { crtView.Filter = FilerOption; } else { crtView.Filter = null; } }
public void creatWindow(bool edit, DataGrid datagrid0) { if (!settingUp) { int p1ID = -1; string p2IDs = "-1"; if (edit) { p1ID = int.Parse(((DataRowView)datagrid0.SelectedItem).Row["ID"].ToString()); p2IDs = ((DataRowView)datagrid0.SelectedItem).Row["对方武将ID"].ToString(); } int temp1 = p1ID; string tempP2IDs = p2IDs; //int temp2 = p2ID; Window win = new Window(); win.Title = "编辑武将关系"; if (!edit) { win.Title = "新增武将关系"; } win.Width = 500; win.Height = 250; win.ResizeMode = ResizeMode.NoResize; Grid grid = new Grid(); win.Content = grid; grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(2, GridUnitType.Star) }); grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(2, GridUnitType.Star) }); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) }); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) }); Person p1 = scen.Persons.GetGameObject(p1ID) as Person; string p1name = p1 == null ? "" : p1.Name; string p2names = ""; GameObjectList list = new GameObjectList(); char[] separator = new char[] { ' ', '\n', '\r', '\t' }; string[] strArray = p2IDs.Split(separator, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < strArray.Length; i++) { Person p2 = scen.Persons.GetGameObject(int.Parse(strArray[i])) as Person; if (p2 != null) { list.Add(p2); p2names = p2names + p2.Name + " "; } } string tempP1name = p1name; string tempP2names = p2names; Label labelP1ID = new Label { Content = "武将ID: " + p1ID, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center }; Label labelP1 = new Label { Content = "武将名称: " + p1name, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center }; Button buttonperson = new Button() { Width = 75, Height = 30, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Content = p1name, IsEnabled = !edit }; Label labelP2ID = new Label { Content = "对方武将ID: " + p2IDs, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center }; Label labelP2 = new Label { Content = "对方武将名称: " + p2names, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center }; Button button2person = new Button() { Width = 75, Height = 30, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Content = p2names }; buttonperson.Click += Buttonperson_Click; button2person.Click += Button2person_Click; void Buttonperson_Click(object sender, RoutedEventArgs e) { Window window = new Window(); window.Title = "请选择武将--双击确认"; window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Width = 800; window.Height = 600; Grid grid2 = new Grid(); window.Content = grid2; grid2.Margin = new Thickness(50); ListBox listBox = new ListBox(); DataGrid dataGrid1 = new DataGrid(); DataTable dt2 = new DataTable(); dt2.Columns.Add("ID", typeof(int)); dt2.Columns.Add("姓名"); dt2.Columns.Add("性别"); dt2.Columns.Add("所在"); dt2.Columns.Add("所属势力"); dt2.Columns.Add("武学"); dt2.Columns.Add("将略"); dt2.Columns.Add("谋略"); dt2.Columns.Add("政理"); dt2.Columns.Add("风度"); DataRow dr1 = dt2.NewRow(); dr1["ID"] = -1; dr1["姓名"] = "无"; dt2.Rows.Add(dr1); foreach (Person person in scen.Persons) { DataRow dr = dt2.NewRow(); dr["ID"] = person.ID; dr["姓名"] = person.Name; dr["性别"] = person.SexString; dr["所在"] = person.Location; dr["所属势力"] = person.BelongedFaction; dr["武学"] = person.Strength; dr["将略"] = person.Command; dr["谋略"] = person.Intelligence; dr["政理"] = person.Politics; dr["风度"] = person.Glamour; dt2.Rows.Add(dr); } dataGrid1.ItemsSource = dt2.DefaultView; dataGrid1.MouseDoubleClick += dataGrid1_MouseDoubleClick; void dataGrid1_MouseDoubleClick(object sender2, MouseButtonEventArgs e2) { if (dataGrid1.SelectedItem != null) { DataTable dt5 = ((DataView)dataGrid1.ItemsSource).ToTable(); p1ID = int.Parse(dt5.Rows[dataGrid1.SelectedIndex]["ID"].ToString()); window.Close(); p1 = scen.Persons.GetGameObject(p1ID) as Person; p1name = p1 == null ? "" : p1.Name; labelP1ID.Content = "武将ID: " + p1ID; labelP1.Content = "武将名称: " + p1name; buttonperson.Content = p1name; } } dataGrid1.IsReadOnly = true; grid2.Children.Add(dataGrid1); dataGrid1.ColumnWidth = new DataGridLength(1, DataGridLengthUnitType.Star); window.ShowDialog(); } void Button2person_Click(object sender, RoutedEventArgs e) { list = new GameObjectList(); strArray = p2IDs.Split(separator, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < strArray.Length; i++) { Person p2 = scen.Persons.GetGameObject(int.Parse(strArray[i])) as Person; if (p2 != null) { list.Add(p2); } } Window window = new Window(); string titleplugin = " 注意:最多只能选一个"; window.Title = "请选择武将-勾选后点击确认" + titleplugin;; window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Width = 800; window.Height = 600; Grid grid2 = new Grid(); window.Content = grid2; grid2.Margin = new Thickness(50); ListBox listBox = new ListBox(); DataGrid dataGrid1 = new DataGrid(); DataTable dt2 = new DataTable(); dt2.Columns.Add("选择", typeof(bool)); dt2.Columns.Add("ID", typeof(int)); dt2.Columns.Add("姓名"); dt2.Columns.Add("性别"); dt2.Columns.Add("所在"); dt2.Columns.Add("所属势力"); dt2.Columns.Add("武学"); dt2.Columns.Add("将略"); dt2.Columns.Add("谋略"); dt2.Columns.Add("政理"); dt2.Columns.Add("风度"); foreach (Person person in scen.Persons) { DataRow dr = dt2.NewRow(); dr["选择"] = false; if (list.HasGameObject(person)) { dr["选择"] = true; } dr["ID"] = person.ID; dr["姓名"] = person.Name; dr["性别"] = person.SexString; dr["所在"] = person.Location; dr["所属势力"] = person.BelongedFaction; dr["武学"] = person.Strength; dr["将略"] = person.Command; dr["谋略"] = person.Intelligence; dr["政理"] = person.Politics; dr["风度"] = person.Glamour; dt2.Rows.Add(dr); } dataGrid1.CanUserAddRows = false; foreach (DataColumn column in dt2.Columns) { if (!column.ColumnName.Equals("选择")) { column.ReadOnly = true; } } dataGrid1.ItemsSource = dt2.DefaultView; dataGrid1.MouseLeftButtonUp += DataGrid1_MouseLeftButtonUp; void DataGrid1_MouseLeftButtonUp(object sender2, MouseButtonEventArgs e2) { if (!bool.Parse(((DataRowView)dataGrid1.SelectedItem).Row["选择"].ToString())) { ((DataRowView)dataGrid1.SelectedItem).Row["选择"] = true; } else if (bool.Parse(((DataRowView)dataGrid1.SelectedItem).Row["选择"].ToString())) { ((DataRowView)dataGrid1.SelectedItem).Row["选择"] = false; } } Button buttonsave2 = new Button() { Width = 50, Height = 25, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Bottom, Content = "确定" }; buttonsave2.Click += Buttonsave2_Click; void Buttonsave2_Click(object sender2, RoutedEventArgs e2) { DataTable dataTable = ((DataView)dataGrid1.ItemsSource).ToTable(); int n = 0; string idssss = ""; string namessss = ""; foreach (DataRow row in dataTable.Rows) { if (bool.Parse(row["选择"].ToString())) { idssss = idssss + row["ID"].ToString() + " "; namessss = namessss + row["姓名"].ToString() + " "; n++; } } if (n > 1) { System.ComponentModel.ICollectionView v = CollectionViewSource.GetDefaultView(dt2); v.SortDescriptions.Clear(); v.SortDescriptions.Add(new System.ComponentModel.SortDescription("选择", System.ComponentModel.ListSortDirection.Descending)); v.Refresh(); dataGrid1.ColumnFromDisplayIndex(0).SortDirection = System.ComponentModel.ListSortDirection.Descending; //dataGrid1.Columns[dt2.Columns["选择"].Ordinal].SortDirection = System.ComponentModel.ListSortDirection.Descending; // dt2.DefaultView.Sort = ("选择 DESC");//这种方法会无法取消已点击列标题产生的排列 MessageBox.Show("此类关系最多只允许选择一个武将"); } else if (n == 0) { p2IDs = "-1"; p2names = ""; window.Close(); labelP2ID.Content = "武将ID: " + p2IDs; labelP2.Content = "武将名称: " + p2names; button2person.Content = p2names; } else { p2IDs = idssss; p2names = namessss; window.Close(); labelP2ID.Content = "武将ID: " + p2IDs; labelP2.Content = "武将名称: " + p2names; button2person.Content = p2names; } } Button buttonexit2 = new Button() { Width = 50, Height = 25, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Bottom, Content = "取消" }; buttonexit2.Click += Buttonexit2_Click; void Buttonexit2_Click(object sender2, RoutedEventArgs e2) { window.Close(); } dataGrid1.Margin = new Thickness(0, 0, 0, 30); grid2.Children.Add(dataGrid1); grid2.Children.Add(buttonsave2); grid2.Children.Add(buttonexit2); dataGrid1.ColumnWidth = new DataGridLength(1, DataGridLengthUnitType.Star); window.ShowDialog(); } Button buttonBack = new Button() { Width = 75, Height = 30, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Content = "还原初始设置" }; buttonBack.Click += ButtonBack_Click; void ButtonBack_Click(object sender, RoutedEventArgs e) { p1ID = temp1; p1name = tempP1name; p2IDs = tempP2IDs; p2names = tempP2names; labelP1ID.Content = "武将ID: " + p1ID; labelP1.Content = "武将名称: " + p1name; buttonperson.Content = p1name; labelP2ID.Content = "武将ID: " + p2IDs; labelP2.Content = "武将名称: " + p2names; button2person.Content = p2names; } Button buttonSave = new Button() { Width = 75, Height = 30, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Content = "保存退出" }; void dicclose1(int P1ID, int P2ID, Dictionary <int, int> dict, Window window) { if (P1ID != -1) { if (dict.ContainsKey(P1ID)) { dict[P1ID] = P2ID; } else { dict.Remove(P1ID); dict.Add(P1ID, P2ID); } } window.Close(); } buttonSave.Click += ButtonSave_Click; void ButtonSave_Click(object sender, RoutedEventArgs e) { Dictionary <int, int> dict = new Dictionary <int, int>(); int p2ID = int.Parse(p2IDs); if (datagrid0.Name == "dgFatherId") { dict = scen.FatherIds; dicclose1(p1ID, p2ID, dict, win); } else if (datagrid0.Name == "dgMotherId") { dict = scen.MotherIds; dicclose1(p1ID, p2ID, dict, win); } else if (datagrid0.Name == "dgSpouseId") { dict = scen.SpouseIds; dicclose1(p1ID, p2ID, dict, win); } initdt(); } Button buttonExit = new Button() { Width = 75, Height = 30, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Content = "直接退出" }; buttonExit.Click += ButtonExit_Click; void ButtonExit_Click(object sender, RoutedEventArgs e) { win.Close(); } grid.Children.Add(labelP1ID); grid.Children.Add(labelP1); grid.Children.Add(buttonperson); grid.Children.Add(labelP2ID); grid.Children.Add(labelP2); grid.Children.Add(button2person); grid.Children.Add(buttonBack); grid.Children.Add(buttonSave); grid.Children.Add(buttonExit); Grid.SetColumn(labelP1ID, 0); Grid.SetRow(labelP1ID, 0); Grid.SetColumn(labelP1, 0); Grid.SetRow(labelP1, 1); Grid.SetColumn(buttonperson, 0); Grid.SetRow(buttonperson, 2); Grid.SetColumn(buttonBack, 1); Grid.SetRow(buttonBack, 0); Grid.SetColumn(buttonSave, 1); Grid.SetRow(buttonSave, 1); Grid.SetColumn(buttonExit, 1); Grid.SetRow(buttonExit, 2); Grid.SetColumn(labelP2ID, 2); Grid.SetRow(labelP2ID, 0); Grid.SetColumn(labelP2, 2); Grid.SetRow(labelP2, 1); Grid.SetColumn(button2person, 2); Grid.SetRow(button2person, 2); win.ShowDialog(); } }
public void ApplyFilters(DataGrid grid, string columnName, object value) { value = AddEscapeSequece(value); if(grid.ItemsSource is DataView) { var itemSource = (DataView)grid.ItemsSource; if (itemSource == null) return; if (!string.IsNullOrEmpty(itemSource.RowFilter)) { string filter = FilterGenerator(itemSource.RowFilter, columnName, value); if (filter != itemSource.RowFilter) { itemSource.RowFilter = CorrectRowFilter(filter); grid.Items.Refresh(); } } else { if (string.IsNullOrEmpty(Convert.ToString(value))) { string dummy = value == null ? null : ""; switch (dummy) { case "": itemSource.RowFilter = "[" + columnName + "]" + " = ''"; break; case null: itemSource.RowFilter = "[" + columnName + "]" + " IS Null"; break; } } else if(itemSource.Table.Columns[columnName].DataType.BaseType.ToString()=="System.Enum") { var value1 = Convert.ToInt32(Enum.Parse(itemSource.Table.Columns[columnName].DataType, Convert.ToString(value))); itemSource.RowFilter = "[" + columnName + "]" + " " + " IN " + "(" + value1 + ")"; } else itemSource.RowFilter = "[" + columnName + "]" + " " + " IN " + "(" + "'" + value + "'" + ")"; } int count = CurrentDistictValues.Count(c => c.IsChecked); if (count == CurrentDistictValues.Count - 1) { CurrentDistictValues[0].IsChecked = true; if (CurrentListBox != null) { CurrentListBox.ItemsSource = CurrentDistictValues; CurrentListBox.UpdateLayout(); CurrentListBox.Items.Refresh(); } } if (CurrentListBox != null) { var clearButton = FindControls.FindChild<Button>(CurrentListBox.Parent, "btnClear"); if (clearButton != null) { clearButton.IsEnabled = CurrentDistictValues.Count(c => c.IsChecked) > 0; } } } else if (CollectionViewSource.GetDefaultView(grid.ItemsSource) != null) { _view = CollectionViewSource.GetDefaultView(grid.ItemsSource); if (!string.IsNullOrEmpty(FilterExpression)) { string filter = FilterGenerator(FilterExpression, columnName, value); if (filter != FilterExpression) { FilterExpression = CorrectRowFilter(filter); grid.Items.Refresh(); } } else { if (string.IsNullOrEmpty(Convert.ToString(value))) { string dummy = value == null ? null : ""; switch (dummy) { case "": FilterExpression = "[" + columnName + "]" + " = ''"; break; case null: FilterExpression = "[" + columnName + "]" + " IS Null"; break; } } else { var readOnlyCollection = (((ListCollectionView)(_view))).ItemProperties; if (readOnlyCollection != null) { var item = readOnlyCollection.First( c => c.Name == columnName); if (item.PropertyType.BaseType != null && item.PropertyType.BaseType.ToString() == "System.Enum") { var value1 = Convert.ToInt32(Enum.Parse(item.PropertyType, Convert.ToString(value))); FilterExpression = "[" + columnName + "]" + " " + " IN " + "(" + value1 + ")"; } else FilterExpression = "[" + columnName + "]" + " " + " IN " + "(" + "'" + value + "'" + ")"; } } } int count = CurrentDistictValues.Count(c => c.IsChecked); if (count == CurrentDistictValues.Count - 1) { CurrentDistictValues[0].IsChecked = true; if (CurrentListBox != null) { CurrentListBox.ItemsSource = CurrentDistictValues; CurrentListBox.UpdateLayout(); CurrentListBox.Items.Refresh(); } } if (CurrentListBox != null) { var clearButton = FindControls.FindChild<Button>(CurrentListBox.Parent, "btnClear"); if (clearButton != null) { clearButton.IsEnabled = CurrentDistictValues.Count(c => c.IsChecked) > 0; } } } }
public List<CheckedListItem> GetDistictValues(DataGrid grid, string columnName) { if(grid.ItemsSource is DataView) { var itemSource = grid.ItemsSource as DataView; var ditictValues = new List<CheckedListItem> { new CheckedListItem {IsChecked = false, Name = "(Select All)", IsSelectAll = "(Select All)"} }; if (itemSource == null) return null; DataTable table = itemSource.Table; List<string> filteredValues = GetFilteredColumnValues(columnName, itemSource); foreach (DataRow row in table.Rows) { object value = row[columnName]; if (ditictValues.Count(c => c.Name.Equals(value) && c.IsSelectAll != "(Select All)") == 0) { if (!string.IsNullOrEmpty(Convert.ToString(value))) { ditictValues.Add(new CheckedListItem { Name = value, IsChecked = filteredValues.Contains("'" + value + "'") }); } else if (value == null) { ditictValues.Add(new CheckedListItem { Name = null, IsChecked = filteredValues.Contains(null) }); } else if (string.IsNullOrEmpty(Convert.ToString(value))) { ditictValues.Add(new CheckedListItem { Name = value, IsChecked = filteredValues.Contains("") }); } } if (ditictValues.Count(c => c.IsChecked) == ditictValues.Count - 1) ditictValues[0].IsChecked = true; CurrentDistictValues = ditictValues; } return ditictValues; } else if (CollectionViewSource.GetDefaultView(grid.ItemsSource) != null) { var ditictValues = new List<CheckedListItem> { new CheckedListItem {IsChecked = false, Name = "(Select All)", IsSelectAll = "(Select All)"} }; ICollectionView view = CollectionViewSource.GetDefaultView(grid.ItemsSource); _view = view; var unquieValues=new HashSet<string>(); foreach (var rowData in grid.ItemsSource) { var propertyValue = rowData.GetType().GetProperty(columnName); if(propertyValue!=null) { var data = propertyValue.GetValue(rowData, null) == null ? null : Convert.ToString(propertyValue.GetValue(rowData, null)); if(!unquieValues.Contains(data)) { unquieValues.Add(data); } } } List<string> filteredValues = string.IsNullOrEmpty(FilterExpression)?new List<string>() : GetFilteredColumnValues(columnName, FilterExpression); foreach(var value in unquieValues) { if (ditictValues.Count(c => c.Name.Equals(value) && c.IsSelectAll != "(Select All)") == 0) { if (!string.IsNullOrEmpty(Convert.ToString(value))) { ditictValues.Add(new CheckedListItem { Name = value, IsChecked = filteredValues.Contains("'" + value + "'") }); } else if (value == null) { ditictValues.Add(new CheckedListItem { Name = null, IsChecked = filteredValues.Contains(null) }); } else if (string.IsNullOrEmpty(Convert.ToString(value))) { ditictValues.Add(new CheckedListItem { Name = value, IsChecked = filteredValues.Contains("") }); } } } CurrentDistictValues = ditictValues; return ditictValues; } return null; }
/// <summary> /// Возвращяет коллекцию для WPFDataGrid /// </summary> /// <returns></returns> public System.Collections.IEnumerable GetSource() { Source = CollectionViewSource.GetDefaultView(Employees); return(Source); }
public static bool IsDefaultView(System.ComponentModel.ICollectionView view) { return(default(bool)); }