将SelectedItem子类列表绑定到文本框

时间:2019-05-29 20:02:56

标签: c# wpf data-binding textbox selecteditem

我想显示一个连接到文本框的字符串列表。我使用转换器,效果很好。问题是当我尝试将选定的项目子类绑定到文本框时,它不显示值。它显示Projectname.Model.Tag

标签是子类

<TextBox HorizontalAlignment="Left"  Height="23" Text="{Binding Path=SelectedItem.Tags,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource myConverter}}" VerticalAlignment="Top" Width="auto"/>

如果我这样做

<TextBox HorizontalAlignment="Left"  Height="23" Text="{Binding Path=SelectedItem.Tags.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource myConverter}}" VerticalAlignment="Top" Width="auto"/>

它只是显示为空

如何将所选项目正确绑定到文本框?

编辑 因此,我有一个Book类,其中包含标签列表。名称是标签的属性。

public class Book:INotifyPropertyChanged
    {

        private int _idBook;
        private string _title;
        private ObservableCollection<Tag> _tag;

        public int IdBook
        {
            get { return _idBook; }
            set { _idBook = value; }
        }

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        public ObservableCollection<Tag> Tags
        {
            get { return _tag; }
            set { _tag = value; OnPropertyChanged("Tags"); }
            }
}

和标记模型

 public class Tag:INotifyPropertyChanged
    {

        private int _idTag;
        private string _name;

        public int IdTag
        {
            get { return _idTag; }
            set { _idTag = value; OnPropertyChanged("IdTag"); }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged("Name"); }
        }
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,答案是在转换器中而不是使用string.Join我正在使用String Builders Append,现在它可以正确绑定了。

相关问题