BInding for Listbox In WPF - wpf

I have a Main window with textbox. I enter some text and click enter it popups a window with listbox and displays the items matching the text entered in the Mainwindow. On selecting the item from the ListBox the text gets set in my textbox of Mainwindow.
I am following the MVVM Pattern. I am not able to set the binding for my listbox in my Mainwindow.(Using CommandBinding)
Does anyone has some solution or sample for the similar scenario?
Thanks

On the ViewModel you need a property SelectedListBoxItem
Bind it to the SelectedItem of your ListBox.
Bind the Text-property to the SelectedListBoxItem of your ViewModel
and that should basically be it.
<ListBox
ItemsSource="{Binding Path=ItemsView, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
SelectedItem="{Binding Path=SelectedListBoxItem, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
/>
<TextBox Text="{Binding Path=SelectedListBoxItem, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
/>

Related

Cannot change appearance of "{NewItemPlaceholder}" in WPF combobox

I have bound an ObservableCollection<Company> to a combobox.
<ComboBox
IsEditable="True"
Text="{Binding Path=Company.CompanyName, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=CompanyCollection,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ucEventDialog}}}"
SelectedItem="{Binding Path=Company}"
HorizontalAlignment="Stretch"/>
During runtime WPF adds at the end of the bound list an additional ComboboxItem {NewItemPlaceHolder} to the dropdown list.
I would like to use this item to trigger an additional dialog box to add a new item to the collection.
I tried to create a template for this additional item using <ComboBox.Resources> ...
<ComboBox.Resources>
<DataTemplate x:Key="PlaceholderTemplate">
<TextBlock Text="New..."/>
</DataTemplate>
</ComboBox.Resources>
However I have not found any means to ...
Change the appearance of the {NewItemPlaceHolder}.
Trigger an event when the {NewItemPlaceHolder} is selected.

WPF ComboBox IsEnabled from Items Count

Im wanting the XAML of a ComboBox to handle its own IsEnabled property based on whether or not it has Items. If the DataTable in the code behind returns Items, id like the ComboBox to be enabled otherwise if no Items are added, it remains or becomes a disabled control. Is this at all possible?
My current ComboBox setup:
<ComboBox x:Name="ImportDate"
DisplayMemberPath="FileDate"
SelectedValuePath="ID"
ItemsSource="{Binding Mode=OneWay}"
SelectedIndex="0"
Style="{DynamicResource sanComboBox_Standard}" />
If you want the ComboBox to be enabled when it has items, and disabled when there are no items, you could just bind IsEnabled to the HasItems property:
<ComboBox x:Name="ImportDate"
DisplayMemberPath="FileDate"
SelectedValuePath="ID"
ItemsSource="{Binding Mode=OneWay}"
SelectedIndex="0"
Style="{DynamicResource sanComboBox_Standard}"
IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}" />

Bind the Text Property of Textblock to a Combobox selectedItem with different DataContext in Silverlight

I have a Data Grid that is bound to a object of type MyStaff. Apart from other properties MyStaff contains a column named LookupID. Now, in my ViewModel I have a collection Named Lookups that have a description for each LookupID.
I have a Template column that has a Textblock in Cell Template and Combobox in CellEdit Template. How do I bind the Textblock so that it dsiplays the description from ComboBox based on LookupID.
I know it would be pretty simple if the datacontext for both the Textblock and ComboBox were simple but that is not the case.
I have tried this but this doesn't work. Any suggestions? Also would appreciate any information on how to best use different Data Context for different controls in Silverlight. For this I have added a static resource pointing to the ViewModel Class.
<sdk:DataGridTemplateColumn Header="Action Point"
Width="500"
CanUserReorder="False"
HeaderStyle="{StaticResource dthFull2}"
IsReadOnly="False">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding ElementName=LookupList,
Path=SelectedItem.Description}"
MinHeight="24"
VerticalAlignment="Top"
Padding="2"
TextTrimming="WordEllipsis"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<my:AutoCompleteComboBox x:Name="LookupList"
FilterMode="Custom" Margin="2,0,0,0"
SelectedValue="{Binding LookupID, Mode=TwoWay}"
SelectedValuePath="LookupID"
ItemsSource="{Binding Path=AnalysisLookupList.Values,
Source={StaticResource ViewModel}}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>

Two way binding in ListBox ItemTemplate

I have a ListBox which DataTemplate is TextBox that can be edited. Where should I set Binding TwoWay? In ItemSource or In TextBox binding? Maybe in both? And how it works inside? Is it inherited? So if I set Binding in ItemSource - is it inherited in TextBox?
<ListBox HorizontalAlignment="Left" ItemsSource="{Binding Commands, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Two way binding should be setup op your item you need to display, in this case the Textbox. Here you need to have coupling back to your datacontext. You might want to consider setting the UpdateSourceTrigger property to PropertChanged.
This way you will always have entered value of your text, even without loosing focus.
The itemsource should not be Two way. One way will do since you probable will be binding to an observable collection. This will only be set once from your datacontext. This will automatically handle the adding and removing of items to your collection.
I'd add your code like this:
<ListBox Margin="10,0,0,0" Width="200" HorizontalAlignment="Left" ItemsSource="{Binding Commands}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Where should I set Binding TwoWay?
You should set this Mode for TextBox like this:
<TextBox Text="{Binding Path=Name, Mode=TwoWay}" />
If I'm not mistaken, the Text property is listed TwoWay mode by default. Therefore, it's construction is not required.
From MSDN:
When used in data-binding scenarios, this property uses the default update behavior of UpdateSourceTrigger.LostFocus.
This means that updates the properties were visible at once, you need to set the property UpdateSourceTrigger to PropertyChanged:
<TextBox Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
So if I set Binding in ItemSource - is it inherited in TextBox?
No, inheritance will not be, because settings of Binding unique for each dependency property. Inheritance happens when using DataContext, but again, settings unique for each property.

Binding a WPF ComboBox to a different ItemsSource within a ListBox DataTemplate

I have a ListBox that contains a textbox and a combobox in its datatemplate:
<ListBox Height="147" Margin="158,29,170,0" Name="PitcherListBox" VerticalAlignment="Top" ItemsSource="{Binding SomeCollectionOfObjects}" Background="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=Name}" />
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to bind the listbox to a collection of objects (which I've done successfully), but I want the combobox in the above datatemplate to have its itemssource set to a local property on the window (array of ints). I still want the combobox to have a two-way bind between its selected item and a property on the collection of objects...
I have the following in code:
PitcherListBox.DataContext = this;
Basically in the end, I want the combobox within the listbox to have a different itemssource than the listbox itself. I can't seem to figure out how to change the ComboBox's ItemsSource in XAML. Can someone provide me some feedback? Thanks!
Try this:
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourWindowTypeHere}}}" />
Note that you need to replace YourWindowTypeHere with the type of the Window containing the LocalArrayOfIntsProperty! Also remember that you will need to define an xml namespace for that type!

Resources