0

请帮帮我,我必须在列表视图中填充多选组合框。我实现了多选组合框并将其集成到 Listview 中。但我无法管理数据绑定部分。

这是我的 XAML 代码

<Window.Resources>
    <Task:Task x:Key="Task"/>
</Window.Resources>
<Grid Width="278">
    <ListView Name="XAxisAttributesList" Padding="2"
  SelectionMode="Single"
  Visibility="Visible" Margin="0,0,22,0"
              DataContext="{Binding Source={StaticResource Task}}"
              ItemsSource="{Binding Path=AllItems}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="X Axis" Width="{Binding ElementName=XAxisAttributesList, Path=ActualWidth}">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <control:MultiSelectComboBox x:Name="MC" Width="150" Height="30" 
                                         DataContext="{Binding Source={StaticResource Task}, Path=AllItems/ModelObject}" 
                                         ItemsSource="{Binding Items}" 
                                         SelectedItems="{Binding SelectedItems}"  
                                         Text="{Binding DisplayTitle}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

任务.cs

public class Task
    {
        string Name;
        static ViewModel modelObject;
        static ObservableCollection<Task> allItems;
        public Task()
        {
            Task.CreateTasks();
        }

        public ObservableCollection<Task> AllItems
        {
            get { return allItems; }
            set { allItems = value; }
        }

        public Task(string Name, ViewModel _modelObject)
        {
            this.Name = Name;
            modelObject = _modelObject;
        }

        public static void CreateTasks()
        {
            ObservableCollection<Task> list = new ObservableCollection<Task>();

            List<string> all = new List<string>();
            all.Add("1");
            all.Add("2");
            all.Add("3");

            List<string> selected=new List<string>();
            selected.Add("1");

            list.Add(new Task("Item1", new ViewModel(new MultiSelectDemo.Attribute(all, selected, "Item1"))));
            list.Add(new Task("Item2", new ViewModel(new MultiSelectDemo.Attribute(all, selected, "Item2"))));
            list.Add(new Task("Item3", new ViewModel(new MultiSelectDemo.Attribute(all, selected, "Item3"))));

            allItems = list;
        }

        public string Option
        {
            get { return this.Name; }
        }
        public ViewModel ModelObject
        {
            get { return modelObject; }
        }
    }

请在此处查看图片

在此处输入图像描述

而不是填充第 1 项、第 2 项和第 3 项,它一直只填充第 3 项

4

1 回答 1

0

实际上有两个问题:
第一个问题是:

DataContext="{Binding Source={StaticResource Task}, Path=AllItems/ModelObject}"

应该DataContext="{Bindind ModelObject}"

第二个问题是不static ViewModel modelObject;应该是静态的。

于 2013-09-15T01:19:06.527 回答