How to Create Scrollbar for a ListBox in C# - c#

I am using silverlight and coding in c# and i have to create a scrollbar on a list displayed.
Currently the list is displayed without scrollbar but it should contain one scrollbar as well.
My code to display the List is:
ListBox lines = new ListBox();
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
Grid.SetColumn(lines, 1);
Grid.SetRow(lines, LoopCount);
childGrid.Children.Add(lines);
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;
lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged);
lines.SelectedIndex = lines.Items.Count - 1;
"lines" variable contains my ListBox and i tried to achieve it throught scrollViewer but i dont know how to proceed further.
(Please note that i have to create selection changed event on selecting the items on GUI displayed on button click) so the code for scrollbar must not influence that procedure. And i have to code in c# only not in xaml. Thanks in advance for the help.

When you are puttig items Inside ScrollViewer You need to set the Content of ScrollViewer to ListBox,
change your code like below,
//Create ScrollViewer which is a child of Grid
var scroll = new ScrollViewer();
//add to the grid
childGrid.Children.Add(scroll);
//create lisbox and set it to the content of scrollviewer which is a Child of ScrollViewer
var listBox = new ListBox();
scroll.Content = listBox;

Related

Visibility.Visible not working

I am creating and binding columns/elements into my datagrid.
I created a DataGridTemplateColumnand in that column i'm inserting a TextBox element.
DataGridTemplateColumn columnFeedbackSupplier = new DataGridTemplateColumn();
var dockPanel = new FrameworkElementFactory(typeof(DockPanel));
DataTemplate cellTemplate = new DataTemplate();
FrameworkElementFactory factoryText = new FrameworkElementFactory(typeof(TextBox));
Binding bindText = new Binding("Supplier");
bindText.Mode = BindingMode.TwoWay;
factoryText.SetValue(TextBox.TextProperty, bindText);
//This is not working
factoryText.SetValue(TextBox.VisibilityProperty, Visibility.Visible);
//
cellTemplate.VisualTree = factoryText;
dockPanel.AppendChild(factoryText);
cellTemplate.VisualTree = dockPanel;
Now when I add the following line, it allows me to edit my selected cell, but it also hides my TextBox on my datagrid until I double click on that cell then my element is visible again. Once that cell loses focus, my TextBox is hidden again.
columnFeedbackSupplier.CellEditingTemplate = cellTemplate;
I can use the following line to make my TextBox always visible, but I would not be able to save my edited values into my datagrid.
columnFeedbackSupplier.CellTemplate = cellTemplate;
Is there a way to make my TextBox always visible, while using CellEditingTemplate?

Dinamically add textboxes to a scrollviewer and retrieve data from them

I'm developing a windows phone app that represents a recipe book, so on the "add recipe" page I would like to add dinamically two textboxes representing ingredient name and quantity if the user decides to add a new ingredient.
My idea is to put the textboxes in a scrollviewer element, but I can't figure out how to add them dinamically from code and also how to retrieve data from the added textboxes: how can I name them so that I can retrieve data and exactly know which ingredient was added first, which second, etc?
Scrollviewer does not have a property to directly add elements such as stackpanel and grid which can directly add another element as child.
But Content property of Scrollviewer can be used to achieve this.
Add the two textboxes to the Grid(ContentPanel) firest and then set Grid as content to the scrollviewer
In code behind:
TextBox tb1 = new TextBox();
tb1.Name = "Name";
tb1.Text = "Ingredient Name";
TextBox tb2 = new TextBox();
tb2.Name = "Quantity";
tb2.Text = "Ingredient Quantity";
Grid gd = new Grid();
gd.Children.Add(tb1);
gd.Children.Add(tb2);
sc1.Content = gd;
In Window1.xaml
<ScrollViewer x:Name="sc1">
</ScrollViewer>

How to add ElementHost to Datagridview Cells in C#?

I want to add ElementHost to my datagridview to bring the designed WPF control to the win form.
My question is how to do this! This is my code so far:
dgv.Columns[0].Name = "controls?";
ElementHost host = new ElementHost();
UserControl1 uc;
uc = new UserControl1(10,20);
host.Name = "test";
host.Dock = DockStyle.Fill;
host.Child = uc;
dgv.Columns.Add(host); //< this gives me error!
I tried both dgv.Columns.Add(host) and dgv.Controls.Add(host) but they are not working.
Is there anyway to do this?
Thanks for the help.
p.s. datagridview should have 10 rows and each row has one usercontrol embedded in each cell.

ListBox display selected Items (Image) in another ListBox (Selection Mode Multiple)

I'm just trying to experiment some ListBox functionality in the SelectionChanged event.
I have the following controls:
1.ListBox : Name = ListBoxSource (I just added the Image in XAML)
2.ListBox : Name = ListBoxDisplay
I just want to iterate and get those items selected from ListBoxSource and display it to ListBoxDisplay. How to do that in the Loop?
The Items on the ListBoxSource are only Image controls and no other controls.
I cannot find any solutions on the net because most of the examples/solutions are using TextBlock, TextBox, or CheckBox ...and no Images.
foreach (Object selectedItem in ListBox1.SelectedItems)
{
// What to do in here to add the selected Images to "ListBoxDisplay"
}
Use this
<ListBox x:Name="ListBoxDisplay"
ItemsSource="{Binding ElementName=ListBoxSource, Path=SelectedItems}"/>
instead of all that code.
Also: Use a DataTemplate and DataBinding to fill the ListBoxes that will make this construction much more robust and flexible.
for(int i=0;i<ListBoxSource.Items.Count;i++)
{
Image currentImageItem = ListBoxSource.Items[i] as Image;
Image image = new Image();
image.Source = currentImageItem.Source ;
ListBoxDisplay.Items.Add(image);
}
Sorry for my mistake this code should work
you must handle other properties like width and height

How to set tooltip for a ListviewItem

I am using a ListView with a few fixed-size columns.
The text in the rows may be too large to fit in the column, so I would like to make it so that when the user hovers over the ListViewItem, it shows a tooltip with more text.
I tried setting the ToolTipText property of the ListViewItem:
ListViewItem iListView = new ListViewItem("add");
iListView.ToolTipText = "Add Expanded";
myListView.Items.Add(iListView);
Unfortunately, it didn't seem to work. How can I get ListViewItems to show ToolTips?
Set the ListView's ShowItemToolTips property to true.
Use ListViewItem.ToolTipText Property
// Declare the ListView.
private ListView ListViewWithToolTips;
private void InitializeItemsWithToolTips()
{
// Construct and set the View property of the ListView.
ListViewWithToolTips = new ListView();
ListViewWithToolTips.Width = 200;
ListViewWithToolTips.View = View.List;
// Show item tooltips.
ListViewWithToolTips.ShowItemToolTips = true;
// Create items with a tooltip.
ListViewItem item1WithToolTip = new ListViewItem("Item with a tooltip");
item1WithToolTip.ToolTipText = "This is the item tooltip.";
ListViewItem item2WithToolTip = new ListViewItem("Second item with a tooltip");
item2WithToolTip.ToolTipText = "A different tooltip for this item.";
// Create an item without a tooltip.
ListViewItem itemWithoutToolTip = new ListViewItem("Item without tooltip.");
// Add the items to the ListView.
ListViewWithToolTips.Items.AddRange(new ListViewItem[]{item1WithToolTip,
item2WithToolTip, itemWithoutToolTip} );
// Add the ListView to the form.
this.Controls.Add(ListViewWithToolTips);
this.Controls.Add(button1);
}

Categories