Bind custom control property with ListBox.Items

2019-09-06 18:17发布

My custom control has the following basic structure:

public class NewTextBox : TextBox
{
    public ItemCollection Items { get; set; }
}

And in XAML I have:

<ListBox Name="listBox1" />
<my:NewTextBox Items="{Binding Path=listBox1.Items}" />

The bind doesn't work in this case. Is the property Items wrong?

1条回答
做个烂人
2楼-- · 2019-09-06 19:18

Your binding is incorrectly. Use the ElementName property in your binding to tell WPF where to look for the data, then bind to the Items property

<my:NewTextBox Items="{Binding ElementName=listBox1, Path=Items}" />
查看更多
登录 后发表回答