trying to add listbox inside listboxitem in WPF - wpf

I am trying to use listbox inside listboxitem in WPF
Here , icTodoList and ActivityListBox are two main Lists. i want to add same number of lists in ActivityListBox as icTodoList and further i want to add one listbox under each listViewItem
The problem that is coming with this code is that it only binds listboxitem not listbox under wrap panel.
please let me know where i am wrong
for (var i = 0; i < icTodoList.Items.Count; i++)
{
ListBox lb = new ListBox();
ListBoxItem lbi = new ListBoxItem();
lb.Name = "InnerActivityList";
lb.Height = 500;
lb.Width = 225;
lb.Background = new SolidColorBrush(Colors.Red);
lbi.Content = lb;
ActivityListBox.Items.Add(lbi);
}

Related

WPF dynamic DatagridComboboxColumn

I dynamically create a DataGridComboboxColum in the code. This works fine, however when I select an item in the Combobox in the Grid it disappears after i leave the combobox.
Here is the code:
MyDataGrid.ItemsSource = ergList;
DataGridComboBoxColumn cb = new DataGridComboBoxColumn();
cb.ItemsSource = data
cb.Header = "Tag";
cb.DisplayMemberPath = "Tag";
MyDataGrid.Columns.Add(cb);
How can i fix this?
You need to bind the selected value in the ComboBox to a property of the item in your ergList:
MyDataGrid.ItemsSource = ergList;
DataGridComboBoxColumn cb = new DataGridComboBoxColumn();
cb.ItemsSource = data
b.Header = "Tag";
cb.DisplayMemberPath = "Tag";
cb.SelectedValueBinding = new Binding("SomePropertyOfAnItemInErgList");
MyDataGrid.Columns.Add(cb);
Make sure that the types of the items in the ComboBox and the property to hold the selected value match.

Binding to dynamically created controls

I'm having trouble binding values to dynamically created controls. When a user loads a custom file and that file will have a unknown number of arguments. Arguments have a Group property that when grouped will dynamically add tabItems to a tabControl. I then loop through the arguments and add a label and, for now, a textbox to a grid inside the tabs. though i intend to use different controls depending on the arument type. I want to bind the argument Value property to the textbox. The tabs, labels and textboxes are added fine but, no value binding
He if my not yet re-factored solution so far;
myTab.Items.Clear();
var args = viewModel.Arguments;
var groups = args.GroupBy(arg => arg.Groups);
foreach (var group in groups)
{
TabItemExt tab = new TabItemExt();
tab.Header = group.Key;
Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
int count = 0;
foreach (var argument in group)
{
RowDefinition newRow = new RowDefinition();
grid.RowDefinitions.Insert(count, newRow);
LabelTextBlock label = new LabelTextBlock();
label.Text = argument.DisplayName;
Grid.SetRow(label, count);
Grid.SetColumn(label, 0);
TextBox textBox = new TextBox();
var binding = new Binding();
binding.Source = viewModel.Arguments[argument.Name];
//binding.Source = argument
binding.Path = new PropertyPath("Value");
textBox.SetBinding(TextBlock.TextProperty, binding);
Grid.SetRow(textBox, count);
Grid.SetColumn(textBox, 1);
grid.Children.Add(label);
grid.Children.Add(textBox);
count += 1;
}
tab.Content = grid;
myTab.Items.Add(tab);
}
textBox.SetBinding(TextBlock.TextProperty, binding);
should have been
textBox.SetBinding(TextBox.TextProperty, binding);
just a little over dependent on intellisense.

How to animate ListBoxItem position

I am having some trouble animating the items in my ListBox. Animations on the OpacityProperty work great but when i try to animate the position of my ListBoxItem it simply does not move an inch (No exceptions are thrown, not even a log message indicating error).
Here is the code i am using:
private void deletAnimation()
{
Todo todoToDelete = App.ViewModel.Todos[1];
Storyboard storyboard = new Storyboard();
DoubleAnimation alphaAnim = new DoubleAnimation();
alphaAnim.From = 1;
alphaAnim.To = 0;
alphaAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
ListBoxItem target = TodoList.ItemContainerGenerator.ContainerFromItem(todoToDelete) as ListBoxItem;
Storyboard.SetTarget(alphaAnim, target);
Storyboard.SetTargetProperty(alphaAnim, new PropertyPath(UIElement.OpacityProperty));
storyboard.Children.Add(alphaAnim);
for (int i = App.ViewModel.Todos.IndexOf(todoToDelete) + 1; i < App.ViewModel.Todos.Count; i++)
{
Todo todo = App.ViewModel.Todos[i];
target = TodoList.ItemContainerGenerator.ContainerFromItem(todo) as ListBoxItem;
DoubleAnimation translateAnim = new DoubleAnimation();
translateAnim.From = target.RenderTransformOrigin.Y;
translateAnim.To = target.RenderTransformOrigin.Y - target.ActualHeight;
translateAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
translateAnim.BeginTime = TimeSpan.FromMilliseconds(500);
Storyboard.SetTarget(translateAnim, target);
Storyboard.SetTargetProperty(translateAnim, new PropertyPath(TranslateTransform.YProperty));
storyboard.Children.Add(translateAnim);
}
storyboard.Begin();
}
Some things i have noticed while debugging:
The RenderTransformOrigin.Y property is always 0 no matter which ListBoxItem is referenced.
The Height property is NaN though the ActualHeight property is 67
The parent property of the ListBoxItem is null
These two things make me wonder if i am given a reference to a ListBoxItem that is not rendered of the screen? But as the Opacity animation works perfectly and all of my list is visible(only contains 3 items at the moment) i do not see how this could be the case.
I have also tried using a PointAnimation and animating the RenderTransformOrigin property directly but this gave the same result (nothing that is).
Any help is appreciated, thanks!
Please check out this link:
http://blogs.msdn.com/b/jasongin/archive/2011/01/03/wp7-reorderlistbox-improvements-rearrange-animations-and-more.aspx
Someone has created a class that you can easily incorporate into your app. This has animations for adding/removing items from your list.
This should save you a lot of time and frustration.

Combo Box in DataGrid WPF

I wanted a combo box in data grid which i added using the following code behind
DataColumn dc;
DataGridNS.DataGridTemplateColumn dgc = new DataGridNS.DataGridTemplateColumn();
dgc.Header = dc.ColumnName;
dgc.Width = 100;
dgc.IsReadOnly = true;
DataTemplate dtm = new DataTemplate();
FrameworkElementFactory outer = new FrameworkElementFactory(typeof(ComboBox));
dtm.VisualTree = outer;
dgc.CellTemplate = dtm;
dtgrdAtlas.Columns.Add(dgc);
i want to bind an array to this combo box.
how do i do that.
This code is in a seperate function where i add columns to data grid and my string array is in seperate function/class.
To bind a list to a ComboBox you need to set the ItemsSource, or Binding a List to the ItemsSource.
Here in this example I set directly the ItemsSource :
MyCombobox.ItemsSource = new List<int> {2, 3, 4 ,5, 6};

Listbox ScrollIntoView problem wpf

I am creating a listbox with itemsource of type dataview. I want to scroll the listbox to praticular item which is not selected. I am using the following code for selected item.
Code:
Binding listbox:
DataView dv = newDt.DefaultView;
dv.Sort = "Count Desc";
lbResult.DataContext = dv;
To get the row based on id:
var selectResult = from mypro in albumDetails.ToTable().AsEnumerable() where mypro.Field<string>("ID")==search.ID select mypro;
if (lbResult.SelectedItem != null)
{
lbResult.ScrollIntoView(**lbResult.Items[0]**);
}
How to get the index if the row in the list box.
Geetha
I don't know what is the albumDetails object. Here is the code to get index from DataView.
DataRow row = dataview.Select("ID='" + search.ID + "'")[0];
int i= dataview.Rows.IndexOf(row);

Resources