Display default selection in silverlight comboxbox - silverlight

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

Related

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

Silverlight Combobox setting the selected item to selected item of datagrid

I have 2 silverlight controls on a form; datagrid which is bound to list of items, combobox which is bound to list of values.
Scenario...when a user selects a row on the grid i want to set the update the combobox so that the value of a property of the selected item in the datagrid is displayed.
My combobox looks like
< ComboBox DisplayMemberPath="Description" x:Name="cbTopics"
Margin="141,170,0,0" VerticalAlignment="Top" Width="300" HorizontalAlignment="Left"
SelectedItem="{Binding ElementName=dataGrid1, Path=SelectedItem.Topic.Description, Mode=TwoWay}"/>
In the load event i set the itemssource of the combo to the list of values. I would like to set the combobox to be description of the item selected in the datagrid. The items in the datagrid is a collection of objects
object Code
the object Code has a property Topic which has a property of Description (hence why I am trying SelectedItem.Topic.Description).
Any ideas as to what I am doing wrong here? I am trying to setup the relationship between the datagrid selected item and the combobox through xaml only.
thx
<ComboBox Height="23" HorizontalAlignment="Left" Margin="141,36,0,0"
Name="cbTopics2" VerticalAlignment="Top" Width="399"
SelectedValue="{Binding ElementName=dataGrid1, Path=SelectedItem.Topic.Description, Mode=TwoWay}"
DisplayMemberPath="Description"
SelectedValuePath="Description" />
The above was the solution.

How to bind to CurrentItem of ICollectionView

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>

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!

Trigger Property on another WPF control event

Suppose I have a ListView and TextBox and they are located
in different containers in the same Window.
Selecting items in the listview I want the TextBox Text property to be updated
in accordance with the listview data bound selected item.
Is it possible in XAML?
Sure... Check this out
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<ListBox x:Name="lb">
<TextBlock Text="Hey"/>
<TextBlock Text="There"/>
</ListBox>
<TextBlock Text="{Binding SelectedItem.Text, ElementName=lb}"/>
</StackPanel>
</Page>
You can bind to SelectedItem. In this case, I'm cheating, because I know it's a TextBlock, but in the real world you'll have to write a DataTemplate (most likely).

Resources