简体   繁体   中英

Silver Light Data not populating in TextBoxes

I am new in Silver Light, I am having a button on datagrid with a link to open PopUp window This is how I am opening the Popup

MyChildPage view = new MyChildPage("123");
                view.Show();

and in Constructor of my Page I am getting Data from Database, This is how I am doing in MyChild Page

public MyChildPage(string id)
{
    InitializeComponent();
    LoadData(id);
    client.GetDataCompleted +=
        new EventHandler<GetDataCompletedEventArgs>(client_GetDataCompleted);
}

private void LoadData(string Id)
{
    client.GetLeadsforUnResolvedAsync(policyId);
}

void client_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
    if (e.Error == null)
    {
        if (e.Result != null)
        {
            DatatoLoad = e.Result;
        }
    }
}

private MyClass _data;
public MyClass DatatoLoad
{
    get { return _data; }
    set { _data = value; }
}

and this is my XAML

<TextBlock Text="Name" Style="{StaticResource FieldHeadingStyle}"
    HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="12,29,0,0" Width="67" />
<TextBox x:Name="DatatoLoad_Name"
    VerticalAlignment="Top" HorizontalContentAlignment="Stretch"
    Margin="12,51,100,0" TabIndex="4"/>
<TextBox x:Name="DatatoLoad_Details"
    HorizontalContentAlignment="Stretch" Margin="12,98,100,0"
    TabIndex="4" VerticalAlignment="Top" />

but the Data is not loading in textboxes, I tried debugging the code and it is returning the Data properly, I put breakpoint on GetDataCompleted Event, The data is appearing in DatatoLoad but populating the TextBoxes,What can be the problem and how can I fix it?

You have to bind the TextProperty of your TextBoxes to the property that contains the loaded data:

<TextBox x:Name="DatatoLoad_Name" ...
    Text="{Binding Path=DatatoLoad.Name}"/>

<TextBox x:Name="DatatoLoad_Details" ...
    Text="{Binding Path=DatatoLoad.Details}"/>

And you have to make sure that your data object is set as the DataContext of your view:

public class MyViewModel
{
    private MyClass _data;
    public MyClass DatatoLoad
    {
        get { return _data; }
        set { _data = value; }
    }
}

...
if (e.Result != null)
{
    this.DataContext = new MyViewModel { DatatoLoad = e.Result };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM