ComboBox which is bound to a db table should initially be blank - xaml

I have a WPF combobox control bound to a edm field in a database table. This works fine except that it enters the first value in the control when it starts which is not wanted. Heres the xaml
<ComboBox x:Name="cbMeasure"
Width="104"
ItemsSource="{Binding Source={StaticResource ddMeasureViewSource}}"
DisplayMemberPath="Measure"
IsSynchronizedWithCurrentItem="True"
SelectedValuePath="Measure"
SelectedValue="{Binding Measure1}"/>
If I just hard code the control it doesn't put the first value in the variable. Heres what that xaml looks like
<ComboBox x:Name="cbMeasure" Width="104" Text="{Binding Measure1}">
<TextBlock Text="one"/>
<TextBlock Text="two"/>
<TextBlock Text="three"/>
<TextBlock Text="four"/>
</ComboBox>
What do I have to do to make the database bound combobox start with an empty value the way the textbox combobox does? This is a problem as it puts the first value in the SelectedValue bound to a variable (Measure1).
The db table ddMeasure looks like:
RID Measure
--- -------
1 One
2 Two
3 Three
4 Four
so "One" in put into the ComboBox selection and Measure1 variable is populated as well.

OK I found the solution to this. You have to set the property IsSynchronizedWithCurrentItem to false which I thought I had tried but I think I just removed that property and the default must be true. So the xaml looks like
<ComboBox x:Name="cbMeasure"
Width="104"
ItemsSource="{Binding Source={StaticResource ddMeasureViewSource}}"
DisplayMemberPath="Measure"
IsSynchronizedWithCurrentItem="False"
SelectedValuePath="Measure"
SelectedValue="{Binding Measure1}"/>
Hope this helps others who try to find info on this type of binding.

Related

How do I get default ComboBox behavior in a DataGridTemplate column?

In my RadDataGrid, I have to use a DataGridTemplateColumn because the Itemssource of my combobox is a collection that is a property of the object represented by the grid row. I've got it working except for the fact that the combo box is always editable. In other words, the box and dropdown arrow are always visible. In the screen shot below, the first column is a DataGridComboBoxColumn. The second is a DataGridTemplateColumn. Neither column has been clicked. (Note the column headings are not in the shot.)
In a regular combobox column on a RadDataGrid the combo box is not visible unless you double click on the column. Until you click, the column just displays the selected item. In my columns, the box and dropdown arrow are always visible, before and after you click in or out of the column.
How can I change this to the typical behavior? I want the user to have to click in the column before the box and dropdown arrow become visible. Before that, the column just display the selected item. Here is my code:
<tg:DataGridTemplateColumn SizeMode="Auto">
<tg:DataGridTemplateColumn.CellContentTemplate>
<DataTemplate>
<ComboBox Width="220"
ItemsSource="{Binding Path=ItemCategory.Items, Mode=OneWay}"
SelectedItem="{Binding Products, Mode=TwoWay}"
SelectedValue="{Binding Products.Id, Mode=OneWay}"
SelectedValuePath="Id"
DisplayMemberPath="ItemName">
</ComboBox>
</DataTemplate>
</tg:DataGridTemplateColumn.CellContentTemplate>
<tg:DataGridTemplateColumn.Header>
<TextBlock.Text = "Item Category"/>
</tg:DataGridTemplateColumn.Header>
</tg:DataGridTemplateColumn>

FlipView shows only first Items

I have some code similar to this:
<FlipView UseTouchAnimationsForAllNavigation="False" ItemsSource="{Binding Records}" SelectedItem="{Binding Record, Mode=TwoWay}">
<FlipView.ItemTemplate>
<DataTemplate x:DataType="Models:Record">
<TextBlock x:Name="debugText" Text="{x:Bind Name}" />
<UC:MyControl Record="{x:Bind Mode=OneWay, Converter={StaticResource myConverter}}" />
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
MyControl needs the date to be converted with myConverter, and then it shows the detail information of Record. It shows an image, the name, etc.
The TextBlock debugText is created for debugging purposes.
When I move the FlipView by hand or by code, the elements are transitioning correctly. I can debug how the SelectedItem is updated correctly, and I can see the debugText updating correctly.
But for some reason, MyControl cycling between the first items. It is showing only the 3 or 4 first items, and after arriving at 3th or 4th, it starts again showing the first one. I debug the converter myConverter, and I can see that it is called 3 or 4 times loading the view. But then it not called anymore, even when I transition to the next elements.
It shows only the 3 first elements when UseTouchAnimationsForAllNavigation="False", and it shows the 4 first elements when UseTouchAnimationsForAllNavigation="True".
I can manage to show all the items correctly, by writting the FlipView.ItemsPanel:
<FlipView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel AreScrollSnapPointsRegular="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</FlipView.ItemsPanel>
But it is not a solution for me, because there are many Records (can be more than 300), and done in this way the page takes so long in loading, and wastes so much memory. When I run it in this mode in a phone, the application even crashes when there are many records.
How can I make it work for more than the 3 or 4 first items?

how do I set selectedvalue and itemssource of combobox within datatemplate

This question is related to MVVM project structure.
For displaying data in a ListView I use a DataTemplate. The ItemsSource for the ListView is an ObservableCollection(of Person). Nothing special so far.
But in the DataTemplate I want to display a the State property of each Person in a ComboBox, where the actual value is displayed, and the user can choose another State.
The State comes from States, that is an ObservableCollection(of State).
In order to show all the States, I need to Bind the DataContext to the "toplevel" of the ViewModel. But how do I come down to the individual person again, that is shown in the DataTemplate? Or do I need another approach for this problem?
In XAML I have this:
<ComboBox DataContext="{Binding DataContext, ElementName=pageRoot}" ItemsSource="{Binding States}" DisplayMemberPath="Description" SelectedValue="{Binding ??????}" SelectedValuePath="ID" />
My question is: what do I need to set in order to Bind the SelectedValue correctly so that it shows the actual State per Person in the text-field of the Combobox?
Got it!
For the ItemsSource of the ComboBox I need the "higher" level of the DataContext hierarchy. Instead of Binding the entire ComboBox to that DataContext, I only needed to Bind the ItemsSource to that level.
The corrected code now looks like so:
<ComboBox
ItemsSource="{Binding DataContext.States, ElementName=pageRoot}"
DisplayMemberPath="Description"
SelectedValue="{Binding State.ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="ID"
Trial and error works out! :-)

How to make a cell in a datagrid readonly based the content on another cell in SL4?

I have two columns, the second column depends on the content on the first column. By default, the second columns is readonly. When I enter some valid value, I want the second column to become editable.
To achive this, I created a cell template and cell edit template on the second column where back ground and read only bound to the first column. On load, the first column is null so my second columns comes correctly as read only. Following is Cell Template for second column, where the background color is set by based on the first column.
<DataTemplate>
<Grid>
<Border Background="{Binding FristColumn,Converter={StaticResource ColorConverter}}"/>
<TextBlock Text="{Binding SecondColumn, Converter={StaticResource NumberFormatter}}" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0"/>
</Grid>
</DataTemplate>
Following is cell edit template for second column to make it editable
<DataTemplate>
<Grid>
<TextBox Text="{Binding SecondColumn, Mode=TwoWay, Converter={StaticResource NumberFormatter}}" Margin="0" HorizontalAlignment="Right" IsReadOnly="{Binding FirstColumn, Converter={StaticResource readOnlyConverter}, ConverterParameter=FirstColumn}" Background="{Binding Depend,Converter={StaticResource ColorConverter}, ConverterParameter=FirstColumn}" />
</Grid>
</DataTemplate>
With these two in place, when enter the valid value in the first column, I was expecting the second column color to change but it does not. But If I double click on the cell then it behaves properly based on the first cell. Is there some thing I am missing?
The problem was, the object collection did not have INotifyPropertyChanged implemented. Once I have INotifyPropertyChanged implemented, the colors and cell become editable and non editable based on the converter.
Hope this helps others.

Multicolumn display combobox within WPF datagrid

I wanted to create custom WPF datagrid control, which allows to have datagridcomboboxcolumn with multicolumn display on selection of combobox, how this can be implemented. Please help
Thanks in advance
Kartheesh.
I'm a little confused to exactly what your trying to ask, but possibly I think it's either of two things;
You want a multi-column display per column. For example you want two or more items per column displayed, e.g. two checkboxes, or textboxs, etc. The following code below displays two combox boxes binded to an object data provider
<dg:DataGridTemplateColumn >
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding item1}" />
<TextBlock Text="{Binding item2}" />
</StackPanel>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
<dg:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{StaticResource mybiglist}"
SelectedValue="{Binding item1}"/>
<ComboBox ItemsSource="{StaticResource mysecondbiglist}"
SelectedValue="{Binding item2}"/>
</DataTemplate>
</dg:DataGridTemplateColumn.CellEditingTemplate>
</dg:DataGridTemplateColumn>
Alternatively perhaps you want one or more columns per combo box, then this should help;
Multi-Column ComboBox in WPF
A Simple Multicolumn Combo Box in WPF