How to bind to CurrentItem of ICollectionView - wpf

I want to a property to the current item of a ICollectionView how can I do it? The ICollectionView is used for binding to a combo box, how can I bind another control to the ICollectionView's selected item?

Check out this cheat sheet. In particular, check out the / binding symbol, which references the current item in a collection view.

Setting IsSynchronizedWithCurrentItem on the ComboBox will update the current item with its selection (not sure if you're already doing this). You can then bind the same collection and access its current item with the binding:
<ComboBox ItemsSource="{Binding Names}" IsSynchronizedWithCurrentItem="True" />
<Button Content="{Binding Path=Names/}"/>

Give your ComboBox a name and bind to it's SelectedItem.
For example:
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding MyList}" />
<Grid DataContext={Binding ElementName=MyComboBox, Path=SelectedItem>
...
</Grid>

Related

Silverlight data bind combobox items and value separately

I can use databinding to bind the contents of a combobox to a collection, or I can bind the selected value in the combobox to a member of a class, but I can't do both at the same time. I want to be able to bind the contents to one thing and the selected value to something else, I guess the combobox can't handle two datacontexts or I'm not specifying them explicitly. Example below, I'd appreciate any help! Thanks.
In XAML:
<ComboBox Name="Combo" ItemsSource="{Binding}"
SelectedValue="{Binding ID, Mode=TwoWay}"/>
In code:
LayoutRoot.DataContext = myClass;
Combo.DataContext = items;
This should do it for you, or at least be close.
<Grid DataContext="{Binding Source=MyObject}">
<ComboBox x:Name="Combo"
ItemsSource="{Binding Source=MyCollection}"
SelectedValue="{Binding Path=ID, Mode=TwoWay}"/>
</Grid>

Silverlight Combobox setting selected item to selected item of a datagrid

I would like to be able to bind a combo box to the selected item of the row selected in a datagrid. I want to be able to do this through xaml only.
What is the binding syntax required for the combobox to bind to the selected item of the data grid?
thx
this should work:
<sdk:DataGrid ItemsSource="{Binding Items}" x:Name="MyGrid" Height="100" VerticalAlignment="Top"/>
<ComboBox
x:Name="Results"
Margin="0,100"
SelectedItem="{Binding SelectedItem, ElementName=MyGrid, Mode=TwoWay}"
ItemsSource="{Binding Items}">
</ComboBox>
You bind the selected item of the combo box to the selected item of the data grid. This is done via the ElementName.
Hope this helps.
TJ

WPF Databinding to ComboBox and also toggle its visibility

There are two issues that iam facing. One is binding a collection to combobox
In code:
private ObservableCollection<string> errList;
Initially its empty and then i add items to it.
In XAML:
<comboBox ItemsSource="{Binding errList}" IsSynchronizedWithCurrentItem="True"
Isnt this enough to get it done. But no items are seen in the combobox.
Second is toggling the visibility of the combobox when items are present.
<combobox Visibility="{ Binding ElementName=Page1, Path=ItemsPresent, Converter={StaticResource booltoVis} }"
ItemsPresent is a property which returns true of errList has items more than 0. But this is not working.
Please Help
I don't think you can bind to a private field, instead after filling your collection you can do the following:
YourComboBoxName.ItemsSource = errList;
For the visibility you need to do self binding like this:
<ComboBox Visibility="{Binding Path=ItemsPresent, RelativeSource={RelativeSource Self}, Converter={StaticResource booltoVis}}"/>

WPF Combobox binding

I got two Comboboxes and both of them have binding with the same Source.
<ComboBox ItemsSource="{Binding Source={StaticResource UsersViewSource}}"
And when I change something in the first one, it reflects also to the second one. And I dunno how to keep their SelectedItem values separately, using the same ItemsSource.
The IsSynchronizedWithCurrentItem property should be set to False:
true if the SelectedItem is always
synchronized with the current item in
the ItemCollection; false if the
SelectedItem is never synchronized
with the current item; null if the
SelectedItem is synchronized with the
current item only if the Selector uses
a CollectionView. The default value is
null.
Here's a sample:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<x:Array x:Key="myStrings" Type="sys:String">
<sys:String>one</sys:String>
<sys:String>two</sys:String>
<sys:String>three</sys:String>
<sys:String>four</sys:String>
<sys:String>five</sys:String>
</x:Array>
</Page.Resources>
<StackPanel Width="200">
<ComboBox IsSynchronizedWithCurrentItem="False" Margin="25"
ItemsSource="{Binding Source={StaticResource myStrings}}" />
<ComboBox IsSynchronizedWithCurrentItem="False" Margin="25"
ItemsSource="{Binding Source={StaticResource myStrings}}" />
</StackPanel>
</Page>
You just need to set the IsSynchronizedWithCurrentItem property to false (by default it's null)
I'd guess (from the name of your binding) that the reason this is happening is that you're binding to a CollectionViewSource (that wraps a collection). This class is a proxy that WPF uses that includes (amongst other things) the selected item of a collection. Obviously if you're sharing this collection between two comboboxes, you're also sharing the selected item.
If you set ItemsSource to something that's not a CollectionViewSource, the control will automatically wrap it in one. So, my suggestion would be to bind directly to a collection instead of wrapping in a CollectionViewSource - or, alternatively, create two CollectionViewSource instances, one for each ComboBox.
You can separately bind the SelectedItem property for each combo box separately.
i.e.
SelectedItem={Binding SelectedItem1}
This way when each one's item gets set, it gets stored into a different place.

Display default selection in silverlight comboxbox

I have a silverlight combobox inside of a dataform as follows:
<dataControls:DataForm x:Name="newScheduleMasterForm" Height="350" Width="450" MinWidth="400"
VerticalAlignment="Top"
CommandButtonsVisibility="None"
Header="Add New Master Schedule"
HorizontalAlignment="Left" AutoGenerateFields="False" ContentLoaded="newScheduleMasterForm_ContentLoaded" >
<dataControls:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataControls:DataField>
<ComboBox x:Name="cbScheduleType" SelectedItem="{Binding Schedule, Mode=TwoWay}" SelectedIndex = "0"
ItemsSource="{Binding GetScheduleTypeValues, Source={StaticResource validDataSource}}"
/>
</dataControls:DataField>
</StackPanel>
</DataTemplate>
</dataControls:DataForm.EditTemplate>
</dataControls:DataForm>
The combobox cbScheduleType ItemsSource has values of "Interior" and Exterior. I am unable to display the default selected value "Interior" in the text box of the combobox. Is there a way to do it.
Thanks in advance
Mohit
The problem is that you're trying to both set SelectedIndex and bind Selected Item. I would just set Schedule to Interior in your constructor in codebehind and remove the SelectedIndex attribute.
There are some problems with data binding the SelectedItem property in the default Silverlight ComboBox control.
One way to work around this by creating a custom control that inherits from ComboBox and adds a SelectedValue dependency property.
There's a good example of this on Rockford Lhotka's blog at:
http://www.lhotka.net/weblog/SilverlightComboBoxControlAndDataBinding.aspx

Resources