Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Silverlight User-controls , +
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:
<UserControl 
    x:Class="Tracker.Controls.DatacardDropbox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices" 
    xmlns:Views="clr-namespace:Tracker.Views" 
    xmlns:Models="clr-namespace:Tracker.Web.Models" 
    xmlns:Services="clr-namespace:Tracker.Web.Services" 
    mc:Ignorable="d"
    >
    <UserControl.Resources>
        <riaControls:DomainDataSource x:Key ="source" AutoLoad="True" QueryName="GetDatacardsQuery" d:DesignData="{d:DesignInstance Models:Datacard, CreateList=true}">
            <riaControls:DomainDataSource.DomainContext>
                <Services:DefectDataContext/>
            </riaControls:DomainDataSource.DomainContext>
        </riaControls:DomainDataSource>
        
    </UserControl.Resources>
    <StackPanel>
        <ComboBox x:Name="Box" VerticalAlignment="Center" HorizontalAlignment="Left" Height="Auto" Width="Auto"
                  ItemsSource="{Binding Data, Source={StaticResource source}}"
                  DisplayMemberPath="Name"
                  SelectedValuePath="ID"
                  >
        </ComboBox>
    </StackPanel>        
</UserControl>
 
It's codebehind with the interesting bit setting the binding in the constructor:
    public partial class DatacardDropbox : UserControl
    {
        public DatacardDropbox()
        {
            InitializeComponent();
 
            Box.SetBinding(ComboBox.SelectedValueProperty, 
                           new Binding() { Source = this, Path = new PropertyPath("ID"), Mode = BindingMode.TwoWay });
        }
 
        public static readonly DependencyProperty IDProperty = DependencyProperty.Register(
            "ID",
            typeof (int),
            typeof (DatacardDropbox),
            new PropertyMetadata(0, new PropertyChangedCallback(OnIDChanged)));
 
        public int ID
        {
            get
            {
                return (int)GetValue(IDProperty);
            }
            set
            {
                SetValue(IDProperty, value);
            }
        }
 
        private static void OnIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //null
        }
    }
 
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>
.
.
.            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
.
.
.
            </Grid.RowDefinitions>
 
            <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}"/>
.
.
.
        </Grid>

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 9:12am
Comments
db7uk at 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 at 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
0 Sergey Alexandrovich Kryukov 504
1 Maciej Los 349
2 Kornfeld Eliyahu Peter 325
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2014
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