开发者

Any ideas on how to make the UI react when a property on a class is changed?

For example, I have this small class:

public class SednaTreeViewItem
{
    public string ValueMember { get; set; }
    public string DisplayMember { get; set; }

    public SednaTreeViewItem(string valueMember, string displayMember)
    {
        ValueMember = valueMember;
        DisplayMember = displayMember;
    }
}

Then I use it in a user control as such:

/// <summary>
/// SednaTreeViewItem that is currently selected in the SednaTreeView.
/// </summary>
public SednaTreeViewItem SelectedItem
{
    get 
    {
        if (ultraTree.SelectedNodes.Count > 0)
        {
            var node = ultraTree.SelectedNodes[0];
            return treeNodes.FirstOrDefault(x => x.ValueMember == node.Key);
        }
        else
            return null;                
    }

    set 
    {
        if (value != null)
        {
            ultraTree.ActiveNode = ultraTree.Nodes[value.ValueMember开发者_运维问答];
            ultraTree.Select();
        }                
    }
}

Then in the actual form that uses this user control, I'd like to capture whenever the .Text is changed in a textbox, to have that text changed as well in the user control, which is a treeview.

private void txtInvestor_TextChanged(object sender, EventArgs e)
{
    treeViewInvestors.SelectedItem.DisplayMember = txtInvestor.Text;
}

The problem is that when I changed the value of .DisplayMember, nothing fires to let the treeview know when to update the display text of the node.

Do you have any suggestions on how to implement this?


Use Control Binding like:

this.txtInvestor.DataBindings.Add("Text", this, "SelectedItem", false, DataSourceUpdateMode.OnPropertyChanged);


You should create an event for your class property and then register that event inside your UI class.

The accepted answer in the following link should help you out:

C# : Creating Events for a Class on GET/SET properties

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜