【问题标题】:Silver Light Data not populating in TextBoxes银光数据未填充在文本框中
【发布时间】:2014-08-13 07:01:57
【问题描述】:

我是 Silver Light 的新手,我在 datagrid 上有一个按钮,带有打开 PopUp 窗口的链接 这就是我打开弹出窗口的方式

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

在我的页面的构造函数中,我从数据库中获取数据,这就是我在 MyChild 页面中所做的事情

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; }
}

这是我的 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" />

但数据没有加载到文本框中,我尝试调试代码并正确返回数据,我在 GetDataCompleted 事件上设置断点,数据出现在 DatatoLoad 但填充文本框,可能是什么问题以及如何可以修吗?

【问题讨论】:

    标签: c# wcf silverlight


    【解决方案1】:

    您必须将TextBoxesTextProperty 绑定到包含加载数据的属性:

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

    并且您必须确保将您的数据对象设置为视图的 DataContext:

    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 };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多