English 中文(简体)
银灯中的数据绑定数据
原标题:Data binding in Silverlight

我有三弦的班级

例如:

class abc
        {

            public string a, b, c;

            public abc(string d, string e, string f)
            {
                a = d;
                b = e;
                c = f;
            }


       };

private void button1_Click(object sender, RoutedEventArgs e)
        {
            abc obj = new abc("abc1","abc2","abc3");

            var MainPage1 = new MainPage();
            MainPage1.DataContext = obj;
        }

当我试图绑上一个文字块时 它并不绑紧

<TextBlock Height="23" HorizontalAlignment="Left" Margin="201,66,0,0" Name="textBlock1" **Text="{Binding Path=a}"** VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="201,107,0,0" Name="textBlock2" **Text="{Binding Path=b}"** VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="201,156,0,0" Name="textBlock3" **Text="{Binding Path=c}"** VerticalAlignment="Top" />

我想我漏了点什么,但需要帮助才能找到它,谢谢。

最佳回答

http://msdn.microsoft.com/en-us/library/ms743643" rel=“nofollow”>MSDN :数据绑定引擎支持任何通用语言运行时间( CLR) 对象的公共属性、次级特性以及索引器。

所以使用属性代替公共字段 :

public class abc
{

   public string a { get; set; }
   public string b { get; set; }
   public string c { get; set; }

   public abc(string d, string e, string f)
   {
      a = d;
      b = e;
      c = f;
   }

}

现在您可以设定它为 DataContext , 例如在您的主页面构建器中 :

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        abc obj = new abc("abc1", "abc2", "abc3");

        this.DataContext = obj;
    }
}
问题回答

您不允许对字段绑定, 使其具有类属性 。





相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签