wpf 在列表视图列子类对象中绑定列表框

本文关键字:列表 绑定 对象 视图 wpf 子类 | 更新日期: 2023-09-27 17:56:02

我有一个属性'AllShobeh'

public struct listViewShobehItem
        {
            string _code;
            string _name;
            string _tell;
            string _address;
            List<string> _atms;
            public string Code
            {
                get
                {
                    return _code;
                }
                set
                {
                    _code = value;
                }
            }
            public string Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    _name = value;
                }
            }
            public string Tell
            {
                get
                {
                    return _tell;
                }
                set
                {
                    _tell = value;
                }
            }
            public string Address
            {
                get
                {
                    return _address;
                }
                set
                {
                    _address = value;
                }
            }
            public List<string> Atms
            {
                get
                {
                    return _atms;
                }
                set
                {
                    _atms = value;
                }
            }
        }
ObservableCollection<listViewShobehItem> _AllShobeh = new ObservableCollection<listViewShobehItem>();
public ObservableCollection<listViewShobehItem> AllShobeh
        {
            get { return _AllShobeh; }
            set
            {
                _AllShobeh = new ObservableCollection<listViewShobehItem>(value);
                OnPropertyChanged("AllShobeh");
            }
        }

我在构造函数中的代码:

AllShobeh = new ObservableCollection<listViewShobehItem>(from a in STATICS.db.shobehs
                                                     select new listViewShobehItem() {Code = a.code,Name=a.name,Tell=a.tell,Address=a.address,Atms=(from at in STATICS.db.atms 
                                                                                                                                                    where at.shobehCode==a.code
                                                                                                                                                    select at.code + " - " + at.name).ToList()});
        listviewShobeh.DataContext = this;
        this.DataContext = this;
        listviewShobeh.ItemsSource = AllShobeh;

我想在列表视图中显示AllShobeh并在列表框中的列表视图的最后一列中显示Atms...

这是我的 Xaml 代码:

<ListView Name="listviewShobeh" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Margin="5" SelectionMode="Single">
            <ListView.View>
                <GridView>
                    <GridViewColumn x:Name="colCode" Header="code" DisplayMemberBinding="{Binding Path=Code}"/>
                    <GridViewColumn Header="name" DisplayMemberBinding="{Binding Path=Name}"/>
                    <GridViewColumn Header="tell" DisplayMemberBinding="{Binding Path=Tell}"/>
                    <GridViewColumn Header="address" DisplayMemberBinding="{Binding Path=Address}"/>
                    <GridViewColumn Header="atms" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Expander>
                                    <ListBox ItemsSource="{Binding ElementName=listviewShobeh,Path=Atms}" DisplayMemberPath="{Binding Path=.}"/>
                                </Expander>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

我无法将Allshobeh.Atms绑定到listBox Atms列...

我在图片中的问题

wpf 在列表视图列子类对象中绑定列表框

从绑定中删除 ElementName 和 DisplayMemberPath。

<DataTemplate>
    <Expander>
        <ListBox ItemsSource="{Binding Path=Atms}" />
    </Expander>
</DataTemplate>

另外,更重要的是,您对DataContext和ViewModel类感到困惑,如果您需要有关这一点的具体帮助,请告诉我。