开发者

Data Binding in Combobox

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 ite开发者_C百科m 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"/>


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}" />


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜