SetBinding() on Custom Image Control - c#

I'm stuck on the SetBinding method.
I would like to have 2 kinds of icons in the table, there will be a boolean property and the shown icon will depend on this property. Here is an example :
The problem is that I canĀ“t change the icon. I've tried to google it for about 3 hours, without success.
My idea was to put there Image control and than change the source dependenig on property, but i couldnt find out how to change image source.
So I tryed to create custom sample with image template and SetBinding and here I am stuck...
int index = 0;
foreach (var item in this.VidContentItems) {
if (item.Active == false) {
this.FindControlInCollection("TrueOrFalse",
this.VidContentItems.ElementAt(index)).SetBinding(?????????);
}
index++;
}
Or maybe I'm totally wrong...

OK i solved it like this: I created a table where was just one image item and i add a control image viewer to the grid of the target table, then i made loop and if the value was false i hided the control.. looks easy but this wasnt possible when i was trying to put there just image control, becouse visibility couldnt be changed for specific control , only for all controls...that stacked me a lot..never use image control!!!
partial void VidContentItemsGrid_Activated() {
int index = 0;
foreach (var item in this.VidContentItems) {
if (item.Active == false) {
this.FindControlInCollection("TrueIconInd", this.VidContentItems.ElementAt(index)).IsVisible = false;
}
index++;
}
}

Related

Windows Form C# tablelayoutpanel scroll up to the top not updating when combofilterbox is used

I have a list of countries to be displayed inside the table layout panel(one col x many rows). A combobox filter exists to filter the different continents and based on the filtered continent name, the list of countries are made visible and hidden inside the table view. However, when you scroll the scroller up and down and then you apply a combo-box filter to a different continent name, the scroller doesn't scroll up to the top. The scroller should return to the first visible country control/component inside the table panel layout.
Has anyone has experienced this issue before. The code looks something like this. Any help will be much appreciated. I have been trying different options, nothing seems to have any effect on the scroller.
<pre>
{
....
if (scrollDirection == ScrollDirection.Up)
{
Control usercontrol = GetFirstVisibleCountryUC();
if (usercontrol != null)
{
tableLayoutPanelCountries.ScrollControlIntoView(usercontrol);
tableLayoutPanelCountries.Invalidate(); //Refresh, Update have tried different options
}
}
.....
}
private Control GetFirstVisibleCountryUC()
{
foreach (CountryUC uc in this.tableLayoutPanelCountries.Controls)
{
if (uc.Visible)
{
return uc;
}
}
return null;
}
</pre>
The scroller should return to the first visible country control/component inside the table panel layout.
If that's exactly what you need, then the following should do the job
tableLayoutPanelCountries.AutoScrollPosition = new Point(0, 0);

Different-sized Tiles in Windows App

I know that I can use the ListView and GridView to create "Tiles"/Items whatever size I want, but how can I create different-sized Tiles for use within my app? This will need to work with a ListView or GridView.
I have tried so many things but I just have absolutely no idea how to do this. Any help will be much appreciated.
In case I haven't described what I am trying to achieve properly, here is a pic:
A simple way is to create a new class inheriting from GridView and override the PrepareContainerForItemOverride method. In which you can set the Column Span and RowSpan to Child item based on the model data. Consider your model class contains the Spanning information.
public class VariableGrid : GridView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
ITileItem tile = item as ITileItem;
if (tile != null)
{
GridViewItem griditem = element as GridViewItem;
if (griditem != null)
{
VariableSizedWrapGrid.SetColumnSpan(griditem, tile.ColumnSpan);
VariableSizedWrapGrid.SetRowSpan(griditem, tile.RowSpan);
}
}
base.PrepareContainerForItemOverride(element, item);
}
}
More information : http://wpfplayground.blogspot.in/2013/03/different-sized-tile-items-in-winrt.html
You need to set ItemsPanel/ItemsPanelTemplate of your list to VariableSizedWrapGrid and set Grid.RowSpan/ColumnSpan of your list items to the values you want. I believe you can do that in ItemContainerStyle of the list control, which is best extracted by right-clicking the control in VS XAML design view or in Blend and selecting "Edit Additional Templates"/"Edit Generated Item Container".

Winforms Listbox or Listview image and text

I'm not very experienced on c#. I'm working with winforms and I'm looking for a way to create something like a list of elements with this template , something like the autocompletion list of visual studio.
Is it possible to do? Shall I use listbox or listview?
EDIT
Sorry my question wasn't clear I don't want to create an autocomplete but what i want to create is something like this a list of things with an icon next to the text of that thing.
As I understand from your question, you can create custom UserControl or create a Form and put ListBox in it. If you use From be sure that you change border style layout, just set it to none. After creation for use it you should create form and show it where you want like this:
FrmAutoComplete x = new FrmAutoComplete();
x.Show();
you can put this form in ToolTipItem and show it.
Good luck.
THis is a quick and dirty example of using images in your Listview control. Since I don;t have a lot of information about what you plan to do, I tried to keep is simple.
In short, you need to load some images into one of the ImageLists (Large or Small) built into the Listview control and assign them keys so that you can assign them to specific list items as you add them.
The trick to this is determining which image to use for a specific list item (assuming there are different images assigned to different list items depending on some differentiating factor. For this example, I used an arbitrary assignment of "cars" or "trucks," and simply decided that the first five items in the list would be cars, and the last five would be trucks. I then assigned each image appropriately, using the image key as I added each listview item. You can do this for more complex scenarios, and when using the image key, it does not matter what order the items are added.
For this use case, you will want to create or use images with dimensions of 16 x 16 pixels. I went ahead and added two images to my project resource file, then simply accessed them using the project Properties.Resources name space. There are other ways to do this as well, but this is the most convenient for me.
Hope that helps.
public partial class Form1 : Form
{
static string CAR_IMAGE_KEY = "Car";
static string TRUCK_IMAGE_KEY = "Truck";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.SetupListview();
this.LoadListView();
}
private void SetupListview()
{
var imgList = new ImageList();
imgList.Images.Add("Car", Properties.Resources.jpgCarImage);
imgList.Images.Add("Truck", Properties.Resources.jpgTruckImage);
var lv = this.listView1;
lv.View = View.List;
lv.SmallImageList = imgList;
}
private void LoadListView()
{
for(int i = 1; i <= 10; i++)
{
string currentImageKey = CAR_IMAGE_KEY;
if(i > 5) currentImageKey = TRUCK_IMAGE_KEY;
var item = this.listView1.Items.Add("Item" + i.ToString(), currentImageKey);
}
}

Why does the ListView refuse to display its columns, items, and subitems (shows only Group)?

The ListView seems to be as cantankerous as a polecat and as temperamental as a [elided by the P.C. police]
With help from - and others, I was able to get a ListView working just as I wanted
it to
(http://stackoverflow.com/questions/11423537/how-to-add-subitems-to-a-listview).
Now I'm converting that simple demo for use in a real app.
I create the columns in the form's Load event:
listViewGroupsItems.Columns.Add(colHeadName);
listViewGroupsItems.Columns.Add(colHeadRate);
...but they don't display. However, there is a blank line above my first item. So why are no text values being displayed. Are they "there" but invisible? Why would they get wiped out?
Anyway, what I want to see is:
column1Title column2Title
Group1Name
Item subitem
Item subitem
Group2Name
Item subitem
Group1Name
Item subitem
Item subitem
Item subitem
Item subitem
...but what I actually see is just:
[blank line]
Group1Name
...that's it!
The ListView's View property is set to Details; otherwise, all of the properties are the default values.
My listview code is:
private void AddGroupsAndItems() {
Dictionary<string, string> GroupsDict = PlatypusData.GetGroupsForTopLevel(Convert.ToInt32(labelTopLevel.Tag));
int currentGroup = 0;
foreach (KeyValuePair<string, string> entry in GroupsDict) {
string GroupNumber = entry.Key;
string GroupName = entry.Value;
listViewGroupsItems.Groups.Add(new ListViewGroup(GroupName, HorizontalAlignment.Left));
Dictionary<string, string> ItemsDict = PlatypusData.GetItemsForGroup(GroupNumber);
foreach (KeyValuePair<string, string> itemEntry in ItemsDict) {
string itemID = itemEntry.Key;
string itemName = itemEntry.Value;
ListViewItem lvi = new ListViewItem(string.Format("{0} ({1})", itemName, itemID));
lvi.SubItems.Add(PlatypusData.GetDuckbillNameForItemId(itemID));
listViewGroupsItems.Items.Add(lvi);
listViewGroupsItems.Groups[currentGroup].Items.Add(lvi);
}
currentGroup++;
}
}
UPDATE
I changed my code in the form's Load event from:
var colHeadName = new ColumnHeader { Text = Resources.RateRequestForm_RateRequestForm_Load_Billing_Account_Client, Width = 160 };
var colHeadRate = new ColumnHeader { Text = Resources.RateRequestForm_RateRequestForm_Load_RatePlan_ID, Width = 120 };
listViewCustomerBillAccountsClients.Columns.Add(colheadName); //colHeadName);
listViewCustomerBillAccountsClients.Columns.Add(colheadRate); //colHeadRate);
...to:
ColumnHeader colheadName = new ColumnHeader();
ColumnHeader colheadRate = new ColumnHeader();
listViewCustomerBillAccountsClients.Columns.Add(colheadName);
listViewCustomerBillAccountsClients.Columns.Add(colheadRate);
...and it made no difference at all.
It would seem that the ColumnHeader constructor should be able to take a string of what it should display, but even when I do that:
ColumnHeader colheadName = new ColumnHeader("This");
ColumnHeader colheadRate = new ColumnHeader("That");
...there is still no change (according to Intellisense or whatever it's called, the string arg is an ImageKey, but I thought I'd try just out of thoroughness/frustration.
Late to the party, but I've just spent some time trying to solve a similar problem. The answer I found was that when clearing the list before filling, you must use
listviewGroupItems.Items.Clear();
not
listviewGroupItems.Clear();
The ListView.Clear() method clears everything from the control--including the columns
I had the same issue today. I had coded listView.Clear() as user2867342 mentioned. I needed to change that to listView.Items.Clear(), but that did not make the columns appear. The columns were there, and I could click on them and resize them, but they were completely blank.
I had a ListView set to Details mode. I also had set the OwnerDraw property to true, because I wanted to paint my own progress bar column. MSDN says the following about the OwnerDraw property (emphasis mine):
A ListView control is normally drawn by the operating system. In order
to customize the appearance of ListView items, subitems, and column
headers, set the OwnerDraw property to true and provide a handler for
one or more of the following events: DrawItem, DrawSubItem,
DrawColumnHeader. This is called owner drawing. When the View property
is set to View.Details, all three events occur; otherwise, only the
DrawItem event occurs.
I had to implement the DrawColumnHeader event. In my case, the defualt worked fine, so the method sets the DrawDefault event parameter to true. After implementing this event handler, the column headers appeared correctly:
...Windows.Forms designer code...
listView.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(this.listView_DrawColumnHeader);
...
private void listView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
You're missing column headers in your code. (fixed)
Per the MSDN:
"If your ListView control does not have any column headers specified and you set the View property to View.Details, the ListView control will not display any items. If your ListView control does not have any column headers specified and you set the View property to View.Tile, the ListView control will not display any subitems."
Granted, you'll probably need to make more adjustments than what you see in my SS, but it at least answers your question as to why you are getting blanks.
Edit (Changed lines, although changing them back to your code didn't skew my successful results):
lvi.Group = listViewGroupsItems.Groups[currentGroup];
listViewGroupsItems.Items.Add(lvi);
One more thing to check I just found:
OwnerDraw must be false.
Copy & Paste from previous project and forgot that I used OwnerDraw

TreeView Not Displaying Images from ImageList

I have a TreeView that displays CheckBoxes:
I want to check if a given directory contains an ".mdf" database and if it does, check whether it is attached on the selected server instance. If the database is attached I display an image against that node, and a different image if it is not attached. Note: The images are .png format, size 32x32...
I populate an ImageList from Properties.Resources
mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);
I then loop through the tree and add the relevant image
public static void RecursiveAddImage(TreeNode treeNode, List<string> attachedList)
{
if (String.Compare(Path.GetExtension(treeNode.Text), ".mdf", true) == 0)
{
string databaseName = treeNode.Text.Replace(".mdf", String.Empty);
if (attachedList.Contains(databaseName))
{
treeNode.ImageIndex = 0;
treeNode.SelectedImageIndex = 0;
}
else
{
treeNode.ImageIndex = 1;
treeNode.SelectedImageIndex = 1;
}
}
foreach (TreeNode node in treeNode.Nodes)
RecursiveAddImage(node, attachedList);
}
The above code goes through the loop with no complaints, finds ".mdf"s and seems to add the relevant ImageIndexes but these do not show up in the TreeView. What am I doing wrong here and can I add the ImageList at design time (something I also can't seem to do)?
I have read several posts and ofcourse the MSDN documantation but I still can't seem to get it working. Any help as always, is much appreciated.
Make sure the TreeView control has the ImageList property set to the correct ImageList reference:
mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);
treeView1.ImageList = mainImageList;
TreeNode.StateImageIndex= 0; would set the imagelist images. Make sure the imagelist is binded to Treeview control as mentioned above.

Categories