我的ComboBox
包含1-4之间的int
值,以及包含特定ListView
的{{1}}。
我尝试根据用户的选择重新创建ItemTemplate
行。
因此,如果他选择1将有1行,如果他选择3将有3行,等等。
这是我的代码:
的Xaml:
ListView
视图模型:
<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>
我的问题是:
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 );
}
}
}
ComboBox
以SelectedIndex
中的1开头作为聋人,我将numberOfDevices设置为1。ComboBox
SelectedIndex
进入ViewModel,似乎构建了ListView
项,但GUI中没有显示它们。
看起来get { return _itemHandler.Items;}
只出现在开头,代码不会再次进入那里。感谢。
答案 0 :(得分:3)
如果您在代码中修复了以下问题,它将起作用:
ViewModel
没有实施INotifyPropertyChanged
SelectedIndex
,而不是绑定到SelectedItem
。由于SelectedIndex
基于0而您的SelectedItem
从1开始,因此默认选择了第二项。NumberOfDevices
属性发生更改时,您不会通知Items
属性已更改。您可以在OnPropertyChanged("Items");
OnPropertyChanged("NumberOfDevices");
来解决此问题
醇>