WPF ListBox items command binding - c#

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.

Related

WinRT XAML Databinding: How to bind to a property to the parent's data context when binding within an ItemTemplate?

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">

Can't Reach ViewModel From Within ListBox

I have a ViewModel set as the highest level DataContext for my wpf application but I can't seem to access it when I jump into a ListBox as the new DataContext is the element of the list. A simple example is below.
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<!--1D List-->
<ListBox ItemsSource="{Binding my_list}">
<ListBox.ItemTemplate>
<DataTemplate>
<!--Individual Elements-->
<TetxBlock Text="{Binding ViewModel_DisplayString}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
That example won't actually work when it comes to the button click as the ViewModel_ClickFunction isn't on the items within the class.
So is there anyway for me to do {Binding parent.selected_item} or something like that? I just need to be able to access the ViewModel from within the ListBox.
The DataContext inside ItemTemplate is actually the item itself. So in this case you have to use RelativeSource to walk up the visual tree (to the ListBox) and change the Path to DataContext.ViewModel_DisplayString:
<TetxBlock Text="{Binding DataContext.ViewModel_DisplayString,
RelativeSource={RelativeSource AncestorType=ListBox}}"/>

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..

How to access a Checkbox inside Listbox?

I have a listbox and I have set the itemstemplate as shown below.
XAML:
<ListBox ItemsSource="{Binding DataList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="CheckBox" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock x:Name="TextBlock" Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,10" FontSize="26.667" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to get which all check box has been selected.Is there any way to get checkbox control for each item so that I can check its IsChecked property.
I can think of a way of binding the IsChecked property.But Is there any other way to do it?
Yes. One way to do is to bind the IsChecked property. And if you are using MVVM, probably that's the right way to do it.
Anyways, if you don't want to go the binding way, and assuming you want to iterate over all items of listbox, and prepare a list of checked items, see if this helps:
WPF - Find a Control from DataTemplate in WPF
If you're already binding to the Title property in the item template then it would certainly make sense to bind to IsChecked, too.
If you really need to, you can walk the visual tree by using the VisualTreeHelper to find the CheckBox instances.
Binding the IsChecked property to a boolean property on your object instance contained within DataList would be the simplest and cleanest way. Alternatively, if you want to avoid code behind, then you could write an attached property.
See also How to access a specific item in a Listbox with DataTemplate?
I bet it cannot be simpler than that:
<ListBox SelectionMode="Multiple" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
Content="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Binding a collection to Listbox

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>

Categories

Resources