Click here to Skip to main content
13,045,641 members (71,971 online)
Rate this:
Please Sign up or sign in to vote.
I've got a very simple user control that is just a combobox that binds its item source to a specific result from a DomainDataContext. All this works well, mostly. The very first time the user control is used, it fails to work properly, every time there after it works fine.

The application is pretty straightforward. Its starts with a grid of data, user selects a particular row and that opens the page with the detail view/editor. The very first time this is done the wrong item is selected. If the user goes back to the previous page, selects the very same item it works properly the second time through.

I don't even know where to begin to look at this issue as what initialization is being done that's different the second time around?

The not so sophisticated code:

The User Control XAML:
        <riaControls:DomainDataSource x:Key ="source" AutoLoad="True" QueryName="GetDatacardsQuery" d:DesignData="{d:DesignInstance Models:Datacard, CreateList=true}">
        <ComboBox x:Name="Box" VerticalAlignment="Center" HorizontalAlignment="Left" Height="Auto" Width="Auto"
                  ItemsSource="{Binding Data, Source={StaticResource source}}"

It's codebehind with the interesting bit setting the binding in the constructor:
    public partial class DatacardDropbox : UserControl
        public DatacardDropbox()
                           new Binding() { Source = this, Path = new PropertyPath("ID"), Mode = BindingMode.TwoWay });
        public static readonly DependencyProperty IDProperty = DependencyProperty.Register(
            typeof (int),
            typeof (DatacardDropbox),
            new PropertyMetadata(0, new PropertyChangedCallback(OnIDChanged)));
        public int ID
                return (int)GetValue(IDProperty);
                SetValue(IDProperty, value);
        private static void OnIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

And some context to see how I expect it to be used:
       <Grid x:Name="grid2" DataContext="{Binding Data, ElementName=bugDomainDataSource}" HorizontalAlignment="Left" Margin="29,40,0,0" VerticalAlignment="Top">
.            </Grid.ColumnDefinitions>
            <sdk:Label Content="Datacard:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
            <Controls:DatacardDropbox Grid.Column="1" Margin="3" Grid.Row="0" ID="{Binding Datacard, Mode=TwoWay}"/>

All of this seems to be pretty straight forward Silverlight with WCF RIA (or so I think, but I'm new to this stuff). It appears that binding to user control properties in Silverlight tends to be a bit of a hassle as as I was working this all out I got tons of hits for items on it, but can't seem to locate one where the binding fails the first time, then works there after.

Any help would be appreciated.
Posted 25-Apr-13 8:12am
db7uk 25-Apr-13 16:49pm
Why are you setting the binding in code and in xaml? Take the code one away for a moment. Also the binding in xaml :ItemsSource="{Binding Data, Source={StaticResource source}}".... what is Data? I take it that is the collection from the RIA service?
Doug Gerard 25-Apr-13 17:14pm
Binding in code because everything I tried XAML wise failed, either could locate the property or wouldn't populate. If you have a better way I'd appreciate to seeing it.

Data is returned from the riaControls:DomainDataSource. It is the combobox choices for the datacard known in the database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100