views:

56

answers:

3

Hi, I binded a database table's primary key to the selectedIndex of a combobox. the problem occurs where the primary key starts from 1 but selectedIndex accepts from 0. I mean, when I want to see the item with ID=1 in database, since it's listed as first element in combobox with index 0, it displays the second element in the list, which is considered with ID=1 in the combobox. Can anyone help me on solving this problem?

Thanks in advance. here's my combobox:

<ComboBox SelectedIndex="{Binding SC.User1.UserID, UpdateSourceTrigger=PropertyChanged }" IsSynchronizedWithCurrentItem="True"
 x:Name="proxyResponsibleUserCmb" ItemsSource="{Binding Users, Mode=OneTime}"
 SelectedItem="{Binding SC.User1.FullName, ValidatesOnDataErrors=True,  UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{x:Null}"  Height="23" VerticalAlignment="Top" HorizontalAlignment="Left" Width="118" Margin="184,3,0,0" Grid.Row="0" Grid.Column="1"/>
+2  A: 

Does setting the property IsSynchronizedWithCurrentItem (in your XAML) to True help?

EDIT Maybe this link will help:

http://social.msdn.microsoft.com/Forums/en/wpf/thread/b4e84ea2-9597-4af1-8d3c-835b972e3d73

Ardman
I am afraid it didn't...
cemregoksu
A: 

Quick workaround via a ValueConverter:

Create a ValueConverter in your codebehind:

// of course use your own namespace...
namespace MyNameSpace
{
public class IndexConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(!(value is int)) // Add the breakpoint here!!
            throw new Exception();
        int newindex = ((int)value - 1;
        return newindex;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException("This method should never be called");
    }
}
}

Then, make it known in your XAML:

//(declare a namespace in your window tag:)
xmlns:myNamespace="clr-namespace:MyNameSpace"

// add:
<Window.Resources>
    <ResourceDictionary>
        <myNamespace:IndexConverter x:Key="indexConverter" />
    </ResourceDictionary>
</Window.Resources>

Then change your binding:

<ComboBox SelectedIndex="{Binding SC.User1.UserID, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource indexConverter}}" ... />

That should do the trick. At least you can debug it by inserting a breakpoint in the IndexConverter.

Martin
Thank you for your solution but ı realized that the trick will fail when a User is deleted from the database and thus the order is not synchronised with combobox's indexes anymore.so I still need another solution to be able to make the right one to be selected.
cemregoksu
Now I can update the selected item -I can see it by debugging- but I can't see it in combobox as selected. There is nothing displayed in combobox. Do you have any idea about why it happens and how it is solved?
cemregoksu
If you want your combobox to update on data changes without reloading the page, you should consider putting a ViewModel in between and bind to its ObservableCollection or at least to some property which can raise an OnPropertyChanged event. My solution is quick'n'dirty for static database data.
Martin
+2  A: 

What about using the ComboBox's SelectedValuePath and DisplayMemberPath, and setting your default item with SelectedValue instead of SelectedItem?

<ComboBox x:Name="proxyResponsibleUserCmb" 
    SelectedValuePath="{Binding UserID}" 
    DisplayMemberPath="{Binding FullName}"
    SelectedValue="{Binding SC.User1.UserId, ValidatesOnDataErrors=True,  UpdateSourceTrigger=PropertyChanged}" 
    ItemsSource="{Binding Users, Mode=OneTime}" />
Rachel