Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
XML
<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:
C#
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:
XML
       <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
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900