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..
Related
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.
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>
I have a GridView for which I set the data context programmatically to the view model instance.
The GridView's ItemsSource is bound to an observable collection (PagesToRead) which is a property on the view model.
Within the GridView.ItemTemplate, binding goes against the observable collection in the ItemsSource, but I want to bind the StackPanel's Background element to a different property on the view model.
I'm looking for the magic <Background="{Binding Path=BackgroundColor, Source=???}"> that will escape the current ItemsSource and bind to the BackgroundColor property on the view model.
Here's the elided XAML:
<Grid>
<GridView x:Name="MainGrid" CanReorderItems="True" CanDragItems="True"
ItemsSource="{Binding Path=PagesToRead}"
<GridView.ItemTemplate>
<DataTemplate >
<StackPanel>
<Background="{Binding Path=BackgroundColor, Source=???}">
<TextBlock Text="{Binding Path=Title}"
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
I got an answer via another avenue (thanks Karl Erickson). What you do is:
<StackPanel Background="{Binding Path=DataContext.TileBackgroundColor,
ElementName=MainGrid">
I would like to bind my listbox to an object without the wpf window binding to a datacontext:
<ListBox Height="Auto" HorizontalAlignment="Left" Margin="0,0,0,0" Name="lstb_logFiles" VerticalAlignment="Stretch" Width="100" SelectionChanged="lstb_threadList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding dateName,StringFormat=\{0\}}" Foreground="Orange" Margin="10,3,0,3" Width="80" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And here is where I setup the binding
public ObservableCollection<FileLog> _logFiles = new ObservableCollection<FileLog>();
lstb_logFiles.DataContext = _logFiles;
This doesn't work, my _logFiles definitely have object in it, but lstb_logFiles does not show any items. What am I doing wrong?
For ListBox to populate its item, you should set ItemsSource and not DataContext.
It should be:
lstb_logFiles.ItemsSource = _logFiles;
and not
lstb_logFiles.DataContext = _logFiles;
you have to set the itemssource to "self"
<ListBox ItemsSource="{Binding}"/>
or instead of setting the datacontext you can set the itemssource directly
public ObservableCollection<FileLog> _logFiles = new ObservableCollection<FileLog>();
lstb_logFiles.ItemsSource= _logFiles;
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>