如何基于ListBox选择更新DataGrid的ItemsSource

时间:2017-05-25 18:56:08

标签: c# wpf xaml mvvm listbox

我有一个ListBox和DataGrid。

当我更改ListBox中的选择时,我正在尝试更改DataGrid的ItemsSource。 示例:如果我选择ListBox中的第一项,则Collection1应绑定到DataGrid的ItemsSource。如果我选择第二项,则应将Collection2绑定到DataGrid的ItemsSource。

以下是我尝试的内容

查看 - Xaml

<ListBox 
    Grid.Column="0" 
    Width="100" 
    ItemsSource="{Binding coll1}" 
    SelectedItem="{Binding SelectedName}">
</ListBox>
<DataGrid 
    Grid.Column="1" 
    ItemsSource="{Binding coll, Mode=TwoWay}">
</DataGrid>`

查看模型 -

public class VM1
    {
        private List<Employee> _coll = new List<Employee>();
        public List<Employee> coll
        {
            get
            {
                return _coll;
            }
            set
            {
                coll = value;
            }
        }

        public List<string> _coll1 = new List<string>();

        public List<string> coll1
        {
            get
            {
                return _coll1;
            }
        }

        private string _selectedName = "";
        public string SelectedName 
        {
            get
            {
                return _selectedName;
            }
            set
            {
                _selectedName = value;
            }
        }

        public VM1()
        {
            _coll.Add(new Employee());
            _coll.Add(new Employee());
            _coll.Add(new Employee());

            _coll1.Add("One");
            _coll1.Add("Two");
            _coll1.Add("Three");
        }

    }

模型 -

 public class Employee
    {
        public Employee()
        {
            Name = "Hello";
        }

        public string Name { get; set; }
    }

2 个答案:

答案 0 :(得分:0)

您需要将DataGrid的ItemsSource绑定到视图模型的属性,该属性将根据SelectedName进行更改。像这样:

public class VM1 : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private IEnumerable _collection;
    private string _selectedName;

    public IEnumerable Collection
    {
        get { return _collection; }
        set
        {
            _collection = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Collection)));
        }
    }

    public string SelectedName 
    {
        get { return _selectedName; }
        set
        {
            _selectedName = value;

            // Set Collection property based on the _selectedName
            if (_selectedName == "A")
                Collection = new[] { 1, 2, 3 }; // Collection 1
            else if (_selectedName == "B")
                Collection = new[] { 4, 5, 6 }; // Collection 2
        }
    }
}

然后将DataGrid绑定到Collection:

ItemsSource="{Binding Path=Collection, Mode=OneWay}"

答案 1 :(得分:0)

你可以这样做,这是为了让你进入正确的轨道:

<强> XAML

<Window.DataContext>
    <local:MainViewModel />
</Window.DataContext>
<StackPanel>
    <ListBox x:Name="lstOfCollections"
             Width="100"
             DisplayMemberPath="Name"
             ItemsSource="{Binding Collection}">
    </ListBox>
    <DataGrid DisplayMemberPath="Name"
              ItemsSource="{Binding ElementName=lstOfCollections,Path=SelectedItem}">
    </DataGrid>
</StackPanel>

这是MainViewModel用于dataContext

public class MainViewModel { public MainViewModel() { //Initializing collections Coll1 = new ObservableCollection<Employee> { new Employee { Name = "Coll1" }, new Employee { Name = "Coll1" }, new Employee { Name = "Coll1" }, }; Coll2 = new ObservableCollection<Employee> { new Employee { Name = "Coll2" }, new Employee { Name = "Coll2" }, new Employee { Name = "Coll2" }, }; Coll3 = new ObservableCollection<Employee> { new Employee { Name = "Coll3" }, new Employee { Name = "Coll3" }, new Employee { Name = "Coll3" }, }; Collection.Add(Coll1); Collection.Add(Coll2); Collection.Add(Coll3); } //collection1 public ObservableCollection<Employee> Coll1 { get; set; } //collection2 public ObservableCollection<Employee> Coll2 { get; set; } //collection3 public ObservableCollection<Employee> Coll3 { get; set; } public ObservableCollection<object> Collection { get; set; } = new ObservableCollection<object>(); }
public class Employee
{
    public string Name { get; set; }
}

和类Employee用于使用一些数据填充这些集合。

{{1}}

[coll1 selected coll2 selected coll3 selected