Binding a collection to Listbox - c#

I basically started today with WPF, and I'm astounded by how difficult it is to do binding. I have an array of TextBoxes, in an ObservableCollection, and just want to bind that in my Listbox, so that they arrange themselves vertically.
I have fiddled around with this for 3 already, can you help?
I'm working in a WPF UserControl, not a window as so many tutorials seem to rely on.

In your C# code, you can do something like this:
myListBox.ItemsSource = myTextBoxesCollection;
Or in your XAML code:
<ListBox ItemsSource="{Binding MyTextBoxesCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Text}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
For the XAML, MyTextBoxesCollection needs to be a public property on your data context. One way to set the DataContext could be, in your constructor:
DataContext = this;

Having an ObservableCollection<TextBox> is alomost always the wrong approach. You likely want an ObservableCollection<string> instead.
Then, in your ListBox (or ItemsCollection) you have the following code:
<ListBox ItemsSource="{Binding MyStrings}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding .}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Make MyTextBoxCollection (your ObservableCollection of textboxes) a public property of your DataContext.
<ListBox ItemsSource=”{Binding MyTextBoxCollection}”>
<ListBox.ItemTemplate>
<DataTemplate><TextBox Text=”{Binding Text}” /></DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Related

WPF ListBox items command binding

I got following listbox
<Page.DataContext>
<self:NewUserViewModel/>
</Page.DataContext>
<ListBox x:Name="PermissionLbox" ItemsSource="{Binding ListFromDataContext}" Height="75" Margin="10,117,10,0" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding .}"></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Why if I change CheckBox definition to:
<CheckBox Command={Binding CommandFromDataContext} Content="{Binding .}"></CheckBox><CheckBox Content="{Binding .}"></CheckBox>
name CommandFromDataContex cannot be resolved although CommandFromDataContext exists in NewUserViewModel. The ListFromDataContext is a property of NewUserViewModel, Command either, but Command is not resolved.
The datacontext of the checkbox isnt the viewmodel any more, but rather the individual item it instances in the listbox.
You need to bind to a RelativeSource and Path to get to the ViewModel again.
See existing answer for details.

How to create a wpf template with nested datatemplate (datatemplate in datatemplate)

I'm totaly new to wpf. I try to create a nested datatemplate like this layout (datatemplate inside another datatemplate). I managed to create a datatemplate and to connect to "Object A" in an ObservableCollection, which worked really well. Now I need to have a nested ObservableCollection in each Object A to display Object B and Object C in columns. But I don't really know how to do that and can't find examples.
Maybe anyone could give me a hint?
Thanks and regards,
Marlene
Assuming you're using a ListView or ListBox to display a collection of Object A (lets call it ObjectACollection), and that Object A has properties named (in this example) ObjectBCollection and ObjectCCollection you could do something like this:
<UserControl xmlns:namespaceA="clr-namespace:MyProj.Models.ObjectANamespace"
xmlns:namespaceB="clr-namespace:MyProj.Models.ObjectBNamespace"
xmlns:namespaceC="clr-namespace:MyProj.Models.ObjectCNamespace" >
<ListBox ItemsSource="{Binding ObjectACollection}">
<ListBox.ItemTemplate>
<DataTemplate DataType="namespaceA:ObjectA">
<StackPanel Orientation="Horizontal">
<ListBox ItemsSource="{Binding ObjectBCollection}">
<ListBox.ItemTemplate>
<DataTemplate DataType="namespaceB:ObjectB">
<TextBlock Text="{Binding ObjectBProperty}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding ObjectCCollection}">
<ListBox.ItemTemplate>
<DataTemplate DataType="namespaceC:ObjectC">
<TextBlock Text="{Binding ObjectCProperty}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl>

wp7 c# get control data context

I have a listbox whose item template is a user control:
<ListBox
x:Name="ChatBox"
Width="450"
ItemsSource="{Binding ChatMessage}">
<ListBox.ItemTemplate>
<DataTemplate>
<local:ChatItem DataContext="{Binding ChatMessage}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
in my code behind:
messages = new ObservableCollection<ChatMessage>(dbMessages);
ChatBox.ItemsSource = messages;
How can I get the bound element, that is, the chat message object from inside the control ?
Thanks for help
your ChatItem usercontrol is automatically got the its Datacontext to ChatMessage Object. it is not required to set it again. and for binding properties of your usercontrol to ChatMessage object properties then your ChatItem properties must be Dependency Properties if you already have them then just bind them to ChatMessage Properties like this.
<ListBox
x:Name="ChatBox"
Width="450"
ItemsSource="{Binding ChatMessage}">
<ListBox.ItemTemplate>
<DataTemplate>
<local:ChatItem DataContext="{Binding ChatMessage}" ChatItemDependencyProperty = "{Binding Path=ChatMessageProperty}" />
</DataTemplate>
</ListBox.ItemTemplate>
hope it helps you..

What WPF control can I use for a list of readonly items on WP7?

Right now I use a ListBox but that allows the user to select an item which I don't want. Is there a way to disable selecting or a more suitable control I can use?
Right now I have this:
<ListBox ItemsSource="{Binding Path=PersonNames}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontSize="20"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Use an ItemsControl, it's a base class without selection. (As it does not provide its own ScrollViewer you may need to add one (either in the template or around the control) if you need scrolling)
See the ListView control.

Showing a dynamic size list in XAML (readonly)

I want to show a list of "properties" in my application. A property is simply a name/value pair. The number of properties is dynamic.
The best way to do this? The only thing I can come up with is creating a ListView with an ItemTemplate. But then the items are selectable, and that's not what I want. If I make the list read-only, it becomes gray. Don't like that either.
Anyone has a better suggestion?
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Properties}">
<ItemsControl.ItemTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Value}"/>
</StackPanel>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
Use a Grid for ItemsControl.ItemsPanel with SharedSizeGroup if you want all items to line up nicely.
Look here:
http://www.codeplex.com/wpg or here
http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!448.entry
They are both implmentations of PropertyGrid from WinForms

Categories

Resources