I'm using a VB power packs data repeater control. I need to bind a list of custom objects to labels inside the repeater. The following code works except for the Tip.User.UserName binding.
How can I bind to a property of an Inner class like Tip.User.UserName
public interface ITip
{
DateTime Date { get; set; }
int Id { get; set; }
int UserId { get; set; }
User User { get; set; }
Group Group { get; set; }
}
public interface IUser
{
string DisplayName { get; set; }
string UserName { get; set; }
}
List<Tip> currentTips = SearchTips(toolTxtSearch.Text, Convert.ToInt32(toolCmbTipGroups.ComboBox.SelectedValue));
lblTipI开发者_如何转开发d.DataBindings.Add(new Binding("Text", currentTips, "Id"));
lblTipUser.DataBindings.Add(new Binding("Text", currentTips, "User.UserName")); // this line doesnot work !!!
repeater.DataSource = currentTips;
Totally depends on what error you're getting because nested property syntax should work for WinForm bindings.
As a work around (maybe the DataRepeater isn't using regular binding mechanisms?) add a property to you ITip implementation to wrap it up:
public string UsersUserName
{
get { return User != null ? User.UserName : null; }
}
Edit: Also don't forget to implement INotifyPropertyChanged on your data objects if you want bindings to update when values do. In this case, send property changed events for both the User property of ITip implementations and the UserName of the IUser implementation.
精彩评论