将ComboBox SelectedIndex的绑定到ListView itempssource



i具有一个 ComboBox,其中包含1-4之间的int值,而包含特定ItemTemplateListView。我正在尝试根据用户的选择重新创建ListView行。因此,如果他选择1条将有1行,如果他选择3行,将有3行,等等。这是我的代码:

xaml:

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
<ComboBox Name="cbNumOfUnits" Width="40" HorizontalAlignment="Left" ItemsSource="{Binding Numbers}" SelectedIndex="{Binding NumberOfDevices}" >
</ComboBox>
<ListView Margin="10" Name="lvDataBinding" ItemsSource="{Binding Items}" BorderBrush="Transparent">
    <ListView.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <Label Name="lbldeviceName" Width="100" Content="{Binding Name}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="5,5,0,0" />
                <xctk:MaskedTextBox x:Name="_maskedTextBox" Width="100" Text="{Binding Address}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Mask="000.000.000.000"  />
            </WrapPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

ViewModel:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App3
{
public class ViewModel
{ 
    private ItemHandler _itemHandler;
    public ViewModel()
    {
        _itemHandler = new ItemHandler();
        _itemHandler.Add( new Item() { Name = "Master", Address = "" } );
        numberOfDevices = 1;
        OnPropertyChanged( "NumberOfDevices" );
        numbers = new ObservableCollection<int>();
        for( int i = 0; i < 4; i++ )
        {
            numbers.Add( i + 1 );
        }
    }

    ObservableCollection<int> numbers;
    public ObservableCollection<int> Numbers 
    {
        get { return numbers; }
        set
        { 
            OnPropertyChanged( "Numbers" );
        }
    }

    public List<Item> items;
    public List<Item> Items
    {
        get { return _itemHandler.Items; }
    }

    private int numberOfDevices = 1;
    public int NumberOfDevices
    {
        get
        {
            return numberOfDevices;
        }
        set
        {
            if( value != numberOfDevices )
            {
                numberOfDevices = value;
                string name = null;

                _itemHandler = new ItemHandler();
                for( int i = 0; i < numberOfDevices + 1; i++ )
                {
                    if( i == 0 )
                    {
                        name = "Master";
                    }
                    else
                    {
                        name = "Slave" + i;
                    }
                    _itemHandler.Add( new Item() { Name = name, Address = "" } );
                }

                OnPropertyChanged( "NumberOfDevices" ); 
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged( string propertyName )
    {
        if( null != PropertyChanged )
        {
            PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
        }
    }
}

public class Item
{ 
    public string Name { get; set; }
    public string Address { get; set; }
}

public class ItemHandler
{
    public ItemHandler()
    {
        Items = new List<Item>();
    }
    public List<Item> Items { get; private set; }
    public void Add( Item item )
    {
        Items.Add( item );
    }
} 
}

我的问题是:

  1. 为什么我的 ComboBox不以 SelectedIndex的1个为deafult开始,我将numberOfdevices设置为1。
  2. 更改ComboBox SelectedIndex进入ViewModel,ListView项目似乎是构建的,但在GUI中没有显示。看来get { return _itemHandler.Items;}仅在开始时出现,并且代码不会再重新进入。

谢谢。

如果修复了代码中的以下问题,则可以使用:

  1. 您的ViewModel无法实现INotifyPropertyChanged
  2. 您应该绑定到SelectedItem,而不是绑定到SelectedIndex。由于SelectedIndex是基于0的,并且您的SelectedItem在1上开始,因此默认情况下选择了第二个项目。
  3. NumberOfDevices更改属性时,您不会通知Items属性已更改。您可以通过在OnPropertyChanged("NumberOfDevices");之后添加OnPropertyChanged("Items");来解决此问题

相关内容

  • 没有找到相关文章