So, I've got a list view, as indicated by the question title. I've got two columns set up: Name and Date Modified. These were added in the designer, here's the code emitted by the designer for reference:
// lstFiles
this.lstFiles.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.clmName,
this.clmDate});
// ...
// clmName
this.clmName.Text = "Name";
this.clmName.Width = 105;
// clmDate
this.clmDate.Text = "Modified";
this.clmDate.Width = 128;
In the designer, this looks beautiful.
The list items themselves are a tiny subclass of ListViewItem that simply extracts some metadata from a file (in this case, the date modified), and adds a sub-item to itself:
class GalleryItem : ListViewItem {
public string File;
public DateTime DateModified;
public GalleryItem(string file) : base(Path.GetFileNameWithoutExtension(file)) {
this.ImageKey = Path.GetExtension(file);
File = file;
DateModified = System.IO.File.GetLastWriteTime(file);
this.SubItems.Add(DateModified.ToString());
}
}
To add items to the list, I simply do this:
lstFiles.BeginUpdate();
lstFiles.Clear();
foreach (String f in files) {
ListViewItem lvi = new GalleryItem(f);
lvi.Group = lstFiles.Groups["grpFiles"]; //this varries
//omitted: check/add icon to list
lstFiles.Items.Add(lvi);
}
lstFiles.EndUpdate();
So, this all works great for Large Icon view, etc:
However, it breaks down on Details view:
There are items in the list (there's a scroll bar). If you click roughly in the column under the red arrow (added in paint), you'll select an item (the upper-right area is an image preview), but you won't see anything selected.
In summary, what am I doing wrong?
I just whipped up a sample to test this:
using System;
using System.Windows.Forms;
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var G1 = new ListViewGroup("Group 1");
var G2 = new ListViewGroup("Group 2");
Application.Run(new Form {
Controls = {
new ListView {
Dock = DockStyle.Fill,
Groups = { G1, G2 },
View = View.Details,
//Columns = { "First", "Second" },
Items = {
new ListViewItem { Text = "One", Group = G1, SubItems = { "1" } },
new ListViewItem { Text = "Two", Group = G2, SubItems = { "2" } },
new ListViewItem { Text = "Three", Group = G2, SubItems = { "3" } },
},
},
},
});
}
}
You will notice that it duplicates the problem. If you uncomment the line that creates the columns it works. This suggests that your columns don't exist.
And while typing this the answer popped into my head:
You are calling ListView.Clear instead of ListView.Items.Clear so you are removing the columns in code.
My understanding is that you are not able to use Groups in Details view. I can't test this right now, so I am going off straight memory. However, try populating your list without the Groups and see what happens. I strongly suspect it is the Group part that is causing the problem.
I had exactly the same problem identified by Tergiver, but there is one more gotcha - when you add the columns, the ListView must be in View.Details mode.
Related
im new at programming and need help here... I want to create a Binding with a Combobox Item.
But the DataBinding is not adding a new DataBind, it overwrites the old one because of the loop. So i want if you select a "Profilname" in the Combobox that the "Path" will be displayed.
But so far, just the last loaded .txt file will be displayed because of the overwrite.
Here is now my question: How to avoid the overwrite of the DataBind in the (foreach)-loop?
For information: There is a folder which contains many .txt-files, which are all called: "profile.txt". The Programm search for all the files with a loop and then search in the files with another loop a line, which contains the word "profile_name". And then the Name has to be displayed in the ComboBox and the Path has to be binded to the "Item"/"Text" in the ComboBox.
I hope this is understandable and sorry if my code is confusing or not very strong written, im learning...
foreach (string profiletxt in Directory.EnumerateFiles(profiledirectory, "profile.txt", SearchOption.AllDirectories))
{
foreach (string line in System.IO.File.ReadAllLines(profiletxt))
{
if (line.Contains("profile_name"))
{
string remLine = line.Remove(0, 15);
string dLine = remLine.Replace("\"", "");
// dataBinding
var listProfiles = new List<Profile>() {
new Profile() {Path = profiletxt, Name = dLine,},
};
materialComboBox1.DataSource = listProfiles;
materialComboBox1.DisplayMember = "Name";
materialComboBox1.ValueMember = "Path";
}
}
if (materialComboBox1.SelectedIndex == -1)
{
MessageBox.Show("Error, couldn't find Profiles");
}
}
public class Profile
{
public string Path { get; set; }
public string Name { get; set; }
}
a ComboBox uses its ItemSource containing the available items. In your inner foreach loop you declare a new profile list for every find of profile item:
var listProfiles = new List<Profile>() {
new Profile() {Path = profiletxt, Name = dLine,},
};
materialComboBox1.DataSource = listProfiles;
Instead, you'd probably like to create a new Profile list before the first foreach loop
var listProfiles = new List<Profile>();
and in the inner loop, add your new finding to the list
listProfiles.Add(new Profile() {Path = profiletxt, Name = dLine});
Then, after the outer loop, you may assign the new ItemSource only once.
There are other newby design flaws in your code:
there should be no need to set DisplayMember and ValueMember in the .xaml.cs "code behind". Rather it belongs into the xaml code itself as these are static.
As a more general advise, consider not doing any kind of "business rules stuff" or data holding in your code behind. Rather you like to separate your UI ("View") from your data ("Model") while a "ViewModel" separates these two and implements the business rules. There are tons of good introductions on this MVVM programming pattern out there.
I have a Visual Studio debugger visualizer project, and when I add a ToolStripComboBox to a ToolStripDropDownMenu, the combobox's items only appear the first time the form is shown.
Like this:
The most basic version of the winform code showing the issue is this:
public class MyVisualizerDialog : Form
{
public MyVisualizerDialog()
{
var toolStripComboBox = new ToolStripComboBox
{
Items = { "One", "Two", "Three" }
};
var toolStripDownDown = new ToolStripDropDownMenu
{
Items = { toolStripComboBox }
};
var toolStrip = new ToolStrip
{
Items =
{
new ToolStripMenuItem("Options")
{
DropDown = toolStripDownDown
}
}
};
Controls.Add(toolStrip);
}
}
Then the visualizer code is simply:
public class MyVisualizer : DialogDebuggerVisualizer
{
protected override void Show(
IDialogVisualizerService windowService,
IVisualizerObjectProvider objectProvider)
{
windowService.ShowDialog(
new MyVisualizerDialog());
}
}
Some extra details:
If I add the ToolStripComboBox to ToolStripMenuItem.DropDownItems, it works fine - it seems to specifically be an issue with having a ToolStripComboBox in a ToolStripDropDown.
If I create and open multiple instances of the same form class in a console app, it works fine.
Once the issue occurs, it keeps occurring - even when I revert the code to the version without the ToolStripDropDown
If I restart Visual Studio, it works the first time the form is shown, then not afterwards.
Any ideas?! Anyone know some wrinkle in the way the IDialogVisualizerService disposes controls or something?!
Thanks for reading :)
It appears that, when the debugger visualizer is closed - which is handled in the debugger side, not in the debuggee side - the DropDown is destroyed but the ToolStripManager doesn't know about it and it finds itself with an invalid handle that it doesn't know how to manage.
Since the ToolStripManager is also active in design mode, this propagates the problem throughout the designer interface: you may find that some DropDown items still work after the debugger visualizer has been closed, but you may not be able to add other ToolStripComboBox items anywhere.
If you insist, also those that appeared to be working may not work anymore.
Note that this misbehavior can translate to ComboBox objects; not directly, but when you try to access their Items collection through the interface.
It may also prevent the Project from compiling.
Explicitly disposing of the Form object created in the debugger visualizer side, can partially solve the problem on the debuggee side, but not, as it turns out, on the debugger visualizer side.
A simple solution is to avoid setting the DropDown object of a ToolStripMenuItem and use a MenuStrip instead, adding Items to a ToolStripDownDown.
Create custom data visualizers
Visualizer Security Considerations
Sample debugger visualizer (simple Image visualizer) to test the good and bad behavior.
▶ Create a Class Library Project, Target Framework set to .Net Framework, AnyCPU profile.
▶ Add a reference to [Visual Studio install Path]\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.DebuggerVisualizers.dll and System.Windows.Forms.
▶ Compile the .dll as Release.
▶ Copy the .dll to the \Common7\Packages\Debugger\Visualizers directory of your current Visual Studio installation path.
▶ Start a debug session, add a breakpoint where an Image/Bitmap property is set/loaded and use the magnifier tool to open a preview.
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.DebuggerVisualizers;
[assembly: DebuggerVisualizer(
typeof(ImageVisualizer.DebuggerSide),
typeof(VisualizerObjectSource), Target = typeof(Image), Description = "Test Visualizer")]
namespace TestVisualizer
{
public class DebuggerSide : DialogDebuggerVisualizer
{
override protected void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
{
var image = (Image)objectProvider.GetObject();
var form = new Form();
form.ClientSize = new Size(image.Width, image.Height);
form.FormBorderStyle = FormBorderStyle.FixedSingle;
form.SuspendLayout();
// ------- WORKING CODE ---------------
var menuStrip = new MenuStrip() { };
var tsComboBox = new ToolStripComboBox { Items = { "One", "Two", "Three" } };
var toolStripDownDown = new ToolStripMenuItem() { Text = "Options" };
toolStripDownDown.DropDownItems.AddRange(new ToolStripItem[] { tsComboBox });
menuStrip.Items.AddRange(new ToolStripItem[] { toolStripDownDown });
// ------- WORKING CODE ---------------
// ------- BAD CODE ---------------
//var toolStripComboBox = new ToolStripComboBox { Items = { "One", "Two", "Three" } };
//var toolStripDownDown = new ToolStripDropDownMenu { Items = { toolStripComboBox } };
//var toolStrip = new ToolStrip {
// Items = { new ToolStripMenuItem("Options") { DropDown = toolStripDownDown } }
//};
// ------- BAD CODE ---------------
var pBox = new PictureBox() { Image = image, Dock = DockStyle.Fill };
//form.Controls.Add(toolStrip);
form.Controls.Add(menuStrip);
form.Controls.Add(pBox);
form.MainMenuStrip = menuStrip;
form.ResumeLayout(false);
form.PerformLayout();
windowService.ShowDialog(form);
form?.Dispose();
}
}
}
So I do have a WinForm in my Programm, which contains a series of each a ComboBox and two TextBoxs. There are atm 8 Lines, but this will increase to a total of at least 32, therefore I would like to work with an Array or similar. How do I do that?
My current working, method is that a create a new array of TextBoxes/ComboBoxes which I assign the designated Elemt of the WinForm, manually. Therefore I have a list like this:
tbGU[0] = tbGU1;
tbGO[0] = tbGO1;
cbS[0] = cbS1;
Of course, this looks awful and isn't great if it's copied many times. Anyone got a Solution to my Problem?
I need to access the SelectedIndex of the ComboBox and the Text of the TextBoxes.
I was hoping that I could avoid having to create all the Elements manually by code.
One simple solution is to use the array initializer syntax:
ComboBox[] cbS = new[] { cbS1, cbS2, cbS3 ... };
Another way of doing this would be to get rid of the variables cbS1, cbS2 ... cBSn altogether and create the controls in a for loop.
ComboxBox[] cbS = new ComboBox[32];
// declare the text box arrays here as well
for (int i = 0 ; i < cbS.Length ; i++) {
cbS[i] = new ComboBox();
cbS[i].Location = ... // use "i" to help you position the control
// configure your combo box ...
this.Controls.Add(cbS[i]);
// do the same for the text boxes.
}
A third way is to create a custom control:
// name this properly!
public class MyControl: UserControl {
public ComboBox CbS { get; }
public TextBox TbGU { get; }
public TextBox TbGO { get; }
public MyControl() {
// create and configure the combo box and text boxes here ...
}
}
Then you can use a for loop to create lots of MyControls.
I have a problem with a ListView. I want each Cell to have a label and a switch but the text of the label does not appear.
Here is my code:
public class FilterPage : ContentPage
{
public FilterPage()
{
List<FilterCell> listContent = new List<FilterCell>();
foreach(string type in Database.RestaurantTypes)
{
FilterCell fc = new FilterCell();
fc.Text = type;
listContent.Add(fc);
}
ListView types = new ListView();
types.ItemTemplate = new DataTemplate(typeof(FilterCell));
types.ItemsSource = listContent;
var layout = new StackLayout();
layout.Children.Add(types);
Content = layout;
}
}
public class FilterCell : ViewCell
{
private Label label;
public Switch CellSwitch { get; private set; }
public String Text{ get { return label.Text; } set { label.Text = value; } }
public FilterCell()
{
label = new Label();
CellSwitch = new Switch();
var layout = new StackLayout
{
Padding = new Thickness(20, 0, 0, 0),
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children = { label, CellSwitch }
};
View = layout;
}
}
If I enter a fixed Text in the FilterCell-Constructor it works fine (e.g.: label.Text = "Hello World")
When I create a Method for the ItemSelected-Event and read out the SelectedItem.Text Property I get the text I assigned as Value but it's never displayed. Only the switch is displayed when I try to run this Code.
Thanks for your help
Niko
Ohh boy. This code looks like a rape (sorry I had to say this).
Now let's see what's wrong:
The reason is you are mixing up data and view heavily.
The line
types.ItemTemplate = new DataTemplate(typeof(FilterCell));
means: "For each item in the list (ItemsSource) create a new filter cell". The FilterCells that you create in the loop are never displayed.
The easy fix
public class FilterPage : ContentPage
{
public FilterPage()
{
var restaurantTypes = new[] {"Pizza", "China", "German"}; // Database.RestaurantTypes
ListView types = new ListView();
types.ItemTemplate = new DataTemplate(() =>
{
var cell = new SwitchCell();
cell.SetBinding(SwitchCell.TextProperty, ".");
return cell;
});
types.ItemsSource = restaurantTypes;
Content = types;
}
}
There is a standard cell type that contains a label and a switch SwitchCell, use it.
As ItemsSource of your list, you have to use your data. In your case the list of restaurant types. I just mocked them with a static list.
The DataTemplate creates the SwitchCell and sets the Databinding for the Text property. This is the magic glue between View and data. The "." binds it to the data item itself. We use it, because our list contains items of strings and the Text should be exactly the string. (read about Databinding: https://developer.xamarin.com/guides/xamarin-forms/getting-started/introduction-to-xamarin-forms/#Data_Binding )
I striped away the StackLayout that contained the list. You can directly set the list as Content of the page.
Lesson
use standard controls, if possible
You should always try to remember to keep data and view apart from each other and use data binding to connect to each other.
Try to avoid unnecessary views.
So I am creating a treeview selector with C#/GTKSharp. I have the basic tree view selector functionality working: The data is loaded into my model and I can click on a node to collapse/expand.
The part I can't work out is how to tell the cell renderer to display the collapse/expand toggle button. In the examples it appears as a triangle that points right or down depending on whether the node is opened or collapsed. I just have a blank space that works as expected as I click but shows nothing.
One possibility is that I have a white on white text issue but I doubt it as my labels show up fine and I have not done any formatting yet.
I tried adding code for ShowExpanders but that was already true.
TreeView = new Gtk.TreeView();
// We add the event handlers (i.e. the control part) to the tree
TreeView.RowActivated += SelectorActivated; //On double click
TreeView.Selection.Changed += SelectorSelected; // On select (single click)
// Raise a context menu here??
// Connect to the ButtonPressEvent
// Raise a popup button
// Create columns [View]
Gtk.TreeViewColumn TreeViewColumTitle = new Gtk.TreeViewColumn();
TreeViewColumTitle.Title = "Profile";
Gtk.CellRendererText NameCellTitle = new Gtk.CellRendererText();
TreeViewColumTitle.PackStart(NameCellTitle, true);
TreeViewColumTitle.SetCellDataFunc(NameCellTitle, new Gtk.TreeCellDataFunc(RenderTitle));
NameCellTitle.Mode = CellRendererMode.Activatable;
// Populate the model
// Note that we could dispense with this step if we generated an ITreeModel
// interface in the Object class.
BindModel(Model);
// Attach everything to the pane
TreeView.Model = GTKModel;
TreeView.AppendColumn(TreeViewColumTitle);
TreeView.ShowExpanders = true;
TreeView.ExpanderColumn.Visible = true;
...
private void BindModel(Model Model) {
GTKModel = new Gtk.TreeStore(typeof(Object));
foreach (Object Object in Model.Selector) {
var BindingData = new BindingDataGTK(this, Object);
BindingData.Iter = GTKModel.AppendValues(Object);
Object.BindingData = BindingData;
BindChildren(GTKModel, BindingData);
}
}
private void BindChildren(TreeStore TreeStore, BindingDataGTK ObjectBinding) {
foreach (var Child in ObjectBinding.Object) {
var BindingData = new BindingDataGTK(this, Child);
BindingData.Iter = TreeStore.AppendValues(ObjectBinding.Iter, Child);
Child.BindingData = BindingData;
BindChildren(TreeStore, BindingData);
}
}
private void RenderTitle(Gtk.TreeViewColumn Column, Gtk.CellRenderer Cell,
Gtk.ITreeModel GTKModel, Gtk.TreeIter Iter) {
Object Object = (Object)GTKModel.GetValue(Iter, 0);
(Cell as Gtk.CellRendererText).Text = Object.Title;
Console.WriteLine("Render {0}", Object.Title);
}
So far as I know this is pretty much an automatic feature, I don't think anything special is needed to make it happen (I've certainly never needed to). You might want to try using a TreeIter to construct your tree instead?
E.g. assuming you already have a TreeView on your form with 0 (zero) columns in it called "treeview" and a list of "MyObject"s called "myListOfObjects"...
treeview.AppendColumn ("Some Title", new CellRendererText(), "text", 0);
Gtk.TreeStore _ts = new TreeStore (typeof(string));
foreach (IMyObject _mo in myListOfObjects) {
Gtk.TreeIter _it = _ts.AppendValues (_mo.SomeText);
RecurseInto (_ts, _it, _mo);
}
treeview.Model = _ts;
...
void RescureInto(Gtk.TreeStore ts, Gtk.TreeIter it, IMyObject mo)
{
foreach (IMyObject _child_mo in mo.Children) {
Gtk.TreeIter _it = ts.AppendValues (it, _child_mo.SomeText);
RecurseInto (ts, _it, _child_mo);
}
}
In theory this should work fine.