Metro app how to create grouped GridView in the C# code behind - c#

In my metro app I need to show groups of VariableSizeWrapGrid on a GridView. Doing this is straight forward in XAML(by creating ItemsPanelTemplate and GroupStyle). But is there a way to do the same in C# code behind.

From here:
using System;
using System.Windows;
using System.Windows.Data;
namespace GroupingSample
{
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
}
CollectionView myView;
private void AddGrouping(object sender, RoutedEventArgs e)
{
myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
if (myView.CanGroup == true)
{
PropertyGroupDescription groupDescription
= new PropertyGroupDescription("#Type");
myView.GroupDescriptions.Add(groupDescription);
}
else
return;
}
private void RemoveGrouping(object sender, RoutedEventArgs e)
{
myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
myView.GroupDescriptions.Clear();
}
}
}
The key here is that you get the default view off of the ItemsSource and set the grouping on that. This line:
myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);

Related

Overridden c# TabControl OnClick is not called

I'm using Microsoft Visual Studio 2013 and writing in c#. I have written a descendant of the TabControl class and overridden the OnClick method, then changed my existing TabControl element to use the new class. Everything compiles and runs, my breakpoint in the constructor is reached, but it's not using the OnClick override! Here's the TabControl descendant code, thanks in advance for any help!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LumaSense.Imaging.Calibration.UI
{
public partial class TabControlModified : TabControl
{
public TabControlModified()
{
InitializeComponent();
}
private bool superuser;
public int lastTabSelectedIndex = 0;
public bool Superuser
{
get { return superuser; }
set { superuser = value; }
}
public int LastTabSelectedIndex
{
get { return lastTabSelectedIndex; }
set
{ this.lastTabSelectedIndex = value; }
}
protected override void OnClick(EventArgs e)
{
// SelectedIndex and tab have already changed before we get here
if (this.superuser == false)
{
if (this.SelectedIndex <= this.lastTabSelectedIndex)
{
this.LastTabSelectedIndex = this.SelectedIndex;
base.OnClick(e);
}
else
{
base.OnClick(e);
this.SelectedIndex = lastTabSelectedIndex;
}
}
else
{
this.LastTabSelectedIndex = this.SelectedIndex;
base.OnClick(e);
}
}
}
}
This snippet works just fine. You need to make sure you have added pages to your tab control in order for the tabs to display and accessible to the user to click.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var mTab = new MyTab();
mTab.Location = new System.Drawing.Point(100, 100);
// The OnClick will only work on the
// Tabs themselves so Pages must be added to display the Tabs.
var mtabPage1 = new System.Windows.Forms.TabPage();
mTab.Controls.Add(mtabPage1);
this.Controls.Add(mTab);
}
class MyTab : TabControl
{
protected override void OnClick(EventArgs e)
{
MessageBox.Show("I was clicked.");
base.OnClick(e);
}
}
}
I found out that using Deselecting and Selecting instead of creating a descendent of TabControl fixed the problem. Somehow, when using the TabControl descendant, adding the Reactive UI binding of TabControlModified .SelectedIndex messed things up and the overridden OnClick was no longer callerd.

list representing character error

I want to know if I can add here names instead of numbers, to display them further in the textbox:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<int> names = new List<int>();
private void Form1_Load(object sender, EventArgs e)
{
nmstxtbx.Text = "";
combo_list.Items.Add("Ahmed");
names.Add("Atef");
combo_list.Items.Add("Omar");
names.Add(5000);
combo_list.Items.Add("Mohamed");
names.Add(4000);
}
public Form1()
{
InitializeComponent();
}
private void combo_list_SelectedIndexChanged(object sender, EventArgs e)
{
nmstxtbx.Text = names[combo_list.SelectedIndex].ToString();
}
}
}
So please am a real noob here, can anyone help, also I was asking what I have to write if I want to create a combo box with some names in it, and want to allow the user to type one of the entries and when he presses enter, the name disappears, can anyone help me with that ?
Not sure what are you trying to achieve but:
names.Add("Atef") - wont works because names is int type - you have to change it to
List<string> names = new List<string>()
For removing an item by entering it into textbox use:
private void nmstxtbx_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
if (combo_list.Items.Contains(nmstxtbx.Text))
combo_list.Items.Remove(nmstxtbx.Text);
}
}
If you need Names and their Number to be related to each other it is much easier to have a class that hold this info together, for example a NameDTO class that holds an Id and a Name.
public class NameDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
You can now use a list of that class in your form to hold all the names:
List<NameDTO> names = new List<NameDTO>();
In the form load event you fill your List and assign it to the Datasource of the Combobox. You can use the DisplayMember and ValueMember properties of the combobox to indicate which properties from the NameDTO you want to see:
private void Form1_Load(object sender, EventArgs e)
{
names.Add( new NameDTO{ Id =2000, Name = "Ahemd"});
names.Add(new NameDTO { Id = 5000, Name = "Omar" });
names.Add(new NameDTO { Id = 4000, Name = "Mohamed" });
comboBox1.DataSource = names;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
}
In your SelectedIndexChanged event you can now get hold of a NameDTO object in the SelectedItem property of the combobox.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var si = (NameDTO) comboBox1.SelectedItem;
nmstxtbx.Text = si.Name;
// or the number is in si.Id
}
To remove the selected item have a button with it its click event:
private void button2_Click(object sender, EventArgs e)
{
names.Remove((NameDTO)comboBox1.SelectedItem);
}

C# WPF page switching

I want to accomplish something like they have in Modern UI for WPF. I have MainWindow : NavigationWindow, which source is page /main.xaml and my code in it looks like this:
public partial class main : Page
{
public main()
{
InitializeComponent();
}
private void settings_Click(object sender, RoutedEventArgs e)
{
settings settingsmenu = new settings();
this.NavigationService.Navigate(settingsmenu);
}
}
The problem is, that when i switch pages, annoying sound appears. I think it's named "navigation start". Can i prevent it from playing ? Or is there another way to switch pages, that doesn't play it ? Thanks in advance.
(sorry if this question is dumb, but I'm new to WPF)
Here is a small workaround. Original sources I found here:
http://social.msdn.microsoft.com/Forums/vstudio/en-us/843677f4-8f0b-46cb-986c-92e8042d0707/stupid-problem-with-webbrowser-control
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Page1.xaml
/// </summary>
public partial class Page1 : Page
{
private RegistryKey regKeyCurrentUser;
private RegistryKey regSubKeyCurrent;
public Page1()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var page2 = new Page2();
this.NavigationService.Navigate(page2);
}
private void Page1_OnLoaded(object sender, RoutedEventArgs e)
{
regKeyCurrentUser = Registry.CurrentUser;
regSubKeyCurrent = regKeyCurrentUser.OpenSubKey(#"AppEvents\Schemes\Apps\Explorer\Navigating\.Current", true);
regSubKeyCurrent.SetValue("", "");
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
var regSubKeyDefault = regKeyCurrentUser.OpenSubKey(#"AppEvents\Schemes\Apps\Explorer\Navigating\.Default");
regSubKeyCurrent.SetValue("", regSubKeyDefault.GetValue("",""));
}
}
}

Trouble trying to create a DatePicker in GTK#

I'm new to GTK# (and desktop development for that matter) and I can't figure out what seems like to be a simple task. :(
I can't get a simple date picker to work. I have a main window with a single text box entry and a single button. When the button is clicked it opens a new window with the calendar widget and when the user double-clicks a date it then should return the selected date to the text box entry on the main window.
Here is my code, what am I missing?
MainWindow.cs
using System;
using Gtk;
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
private DateTest1.CalendarTest datePicker;
protected void OnButton1Clicked (object sender, EventArgs e)
{
datePicker = new DateTest1.CalendarTest();
datePicker.DestroyEvent += new DestroyEventHandler(datePickerDestroyed);
datePicker.ShowAll();
}
public void datePickerDestroyed(object sender, EventArgs e)
{
entry1.Text = datePicker.DatePicked.ToString();
}
}
CalendarTest.cs
using System;
namespace DateTest1
{
public partial class CalendarTest : Gtk.Window
{
public DateTime DatePicked;
public CalendarTest () :
base(Gtk.WindowType.Toplevel)
{
this.Build ();
}
protected void OnCalendar1DaySelectedDoubleClick (object sender, EventArgs e)
{
var datePicker = (Gtk.Calendar)sender;
DatePicked = datePicker.Date;
this.Destroy();
}
}
}
You have to use the Destroyed event, not the DestroyEvent ;)
That is, use this:
datePicker.Destroyed += new EventHandler(datePickerDestroyed);
See also this question.

How to clear all data in a listBox?

I am after a statement that will clear all strings / data that is currently in a listBox, I have tried:
private void cleanlistbox(object sender, EventArgs e)
{
listBox1.ResetText();
}
What about
listbox1.Items.Clear();
If it is bound to a Datasource it will throw an error using ListBox1.Items.Clear();
In that case you will have to clear the Datasource instead. e.g., if it is filled with a Datatable:
_dt.Clear(); //<-----Here's the Listbox emptied.
_dt = _dbHelper.dtFillDataTable(_dt, strSQL);
lbStyles.DataSource = _dt;
lbStyles.DisplayMember = "YourDisplayMember";
lbStyles.ValueMember = "YourValueMember";
Try this:
private void cleanlistbox(object sender, EventArgs e)
{
listBox1.DataSource = null;
listBox1.Items.Clear();
}
private void cleanlistbox(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
This should work:
listBox1.Items.Clear();
this should work:
private void cleanlistbox(object sender, EventArgs e)
{
listBox1.Items.Clear( );
}
Use this:
listBox1.Items.Clear();
Try
private void cleanlistbox(object sender, EventArgs e)
{
ListBox1.Items.Clear();
}
If you are using CListBox as pointer (*) use this line:
pList1->ResetContent();
If your listbox is connected to a LIST as the data source, listbox.Items.Clear() will not work.
I typically create a file named "DataAccess.cs" containing a separate class for code that uses or changes data pertaining to my form. The following is a code snippet from the DataAccess class that clears or removes all items in the list "exampleItems"
public List<ExampleItem> ClearExampleItems()
{
List<ExampleItem> exampleItems = new List<ExampleItem>();
exampleItems.Clear();
return examplelistItems;
}
ExampleItem is also in a separate class named "ExampleItem.cs"
using System;
namespace // The namespace is automatically added by Visual Studio
{
public class ExampleItem
{
public int ItemId { get; set; }
public string ItemType { get; set; }
public int ItemNumber { get; set; }
public string ItemDescription { get; set; }
public string FullExampleItem
{
get
{
return $"{ItemId} {ItemType} {ItemNumber} {ItemDescription}";
}
}
}
}
In the code for your Window Form, the following code fragments reference your listbox:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Windows.Forms;
namespace // The namespace is automatically added by Visual Studio
{
public partial class YourFormName : Form
{
List<ExampleItem> exampleItems = new List<ExampleItem>();
public YourFormName()
{
InitializeComponent();
// Connect listbox to LIST
UpdateExampleItemsBinding();
}
private void UpdateUpdateItemsBinding()
{
ExampleItemsListBox.DataSource = exampleItems;
ExampleItemsListBox.DisplayMember = "FullExampleItem";
}
private void buttonClearListBox_Click(object sender, EventArgs e)
{
DataAccess db = new DataAccess();
exampleItems = db.ClearExampleItems();
UpdateExampleItemsBinding();
}
}
}
This solution specifically addresses a Windows Form listbox with the datasource connected to a list.
In C# Core DataSource does not exist, but this work fine:
listbox.ItemsSource = null;
listbox.Items.Clear();

Categories

Resources