silverlight中的数据网格绑定
我有一节简单的abc课silverlight中的数据网格绑定,silverlight,data-binding,gridview,Silverlight,Data Binding,Gridview,我有一节简单的abc课 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;
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;
}
}
public MainPage()
{
InitializeComponent();
abc obj = new abc("abc1", "abc2", "abc3");
LayoutRoot.DataContext = obj;
}
一个包含三个文本框的网格1 2 3我试图将一个类的这三个属性绑定到一个网格用户控件
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Height="27" HorizontalAlignment="Left" Margin="125,86,0,0" Name="textBox1" Text="{Binding Path= a}" VerticalAlignment="Top" Width="120" />
<TextBox Height="25" HorizontalAlignment="Left" Margin="21,192,0,83" Name="textBox2" Text="{Binding Path= b}" Width="120" />
<TextBox Height="25" HorizontalAlignment="Left" Margin="250,192,0,0" Name="textBox3" Text="{Binding Path= c}" VerticalAlignment="Top" Width="120" />
</Grid>
它不会显示任何错误,但不会显示任何屏幕输出,它会产生什么具体问题 首先,您的类型“abc”应该实现INotifyPropertyChanged
public class abc : INotifyPropertyChanged
{
...
}
然后需要引发INotifyPropertyChanged.PropertyChanged事件
private void RaiseProperty(string propertyName)
{
var handle = PropertyChanged;
if(handle != null)
{
handle(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _a;
public string a { get{ return _a;} set{ _a = value; RaiseProperty("a"); } }
....
如果您正在使用CLR属性,那么这应该可以作为通知绑定的机制使用;该机制是由INotifyPropertyChanged接口提供的请尝试在绑定表达式中不使用Path=和空格。试用:
Text="{Binding a}"
路径隐藏在绑定表达式中。您需要阅读一些关于绑定的参考资料。Layoutroo是我的网格的名称。在上面的示例中需要吗?谢谢先生。因为我是silverlight的新手。我下一步正在做这件事。一般来说,你会实现INotifyPropertyChanged,但上面没有要求,也不是问题的原因。同意Preet Sangha和Phil。唯一的问题是类型不是公共的。