开发者

How to databind to a label with a custom object collection in C# winforms

开发者 https://www.devze.com 2023-01-06 05:39 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消