无法在按钮上的列表框中添加项目单击

时间:2012-09-17 09:32:09

标签: wpf listview mvvm listboxitem

我是MVVM的新手。我在主窗口内有两个网格,其中一个网格包含一个朝向左侧的列表框,另一个网格包含列表视图和两个按钮。

我可以将项目添加到listview,甚至可以弄清楚如何从列表视图中获取所选项目。一旦我在列表视图上选择项目并单击一个按钮(连接),向左侧的列表框将更新我从viewmodel类添加的项目。

以下视图显示了我左侧的列表框:

<Grid Grid.Column="0" Name="BoardTabSelect" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <ListBox Name="ButtonPanel" 
             ItemsSource="{Binding BoardTabs, Mode=TwoWay}" 
             SelectedItem="{Binding SelectedTab, Mode=TwoWay}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Name="BoardtabChanger" 
                               Margin="53,27,0,0" 
                               Text="{Binding TabOperation}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

以下视图显示向右的列表视图以及2个按钮:

<Grid Grid.Row="0" Name="MainConnectGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
    <ListView Name="listView" 
              ItemsSource="{Binding Products}" 
              SelectedItem="{Binding SelectedProduct, Mode=TwoWay}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="300" 
                                Header="Name" 
                                DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Width="283" 
                                Header="Connection Status" 
                                DisplayMemberBinding="{Binding Connection_Status}" />
            </GridView>
        </ListView.View>
    </ListView>

    <Button Content="Connect" 
            Height="23" Width="100" 
            HorizontalAlignment="Left" VerticalAlignment="Top"
            Margin="55,519,0,0" 
            Name="ConnectBtnGrid"  
            Command="{Binding Path=ConnectCommand}" />                        
    <Button Content="Disconnect" 
            Height="23" Width="100"
            HorizontalAlignment="Left" VerticalAlignment="Top" 
            Margin="446,519,0,0" 
            Name="DisconnectBtn" 
            Command="{Binding Path=DisconnectCommand}" />
</Grid>

查看模型:

public class ProductViewModel : INotifyPropertyChanged 
{
    public List<Product> m_Products;
    public List<Product> m_BoardTabs;                

    public ProductViewModel()
    {           
        m_Products = new List<Product>()
        {                
            new Product() {Name = "Bavaria", Connection_Status = "Disconnected"},
            new Product() {Name = "Redhook", Connection_Status = "Disconnected"},                
        };

        m_BoardTabs = new List<Product>()
        {
            new Product() {TabOperation = "Connect"}                 
        };
    }                      

    public List<Product> Products { get; set; }

    public List<Product> BoardTabs { get; set; }

    private Product m_SelectedItem;
    public Product SelectedProduct
    {
        get { return m_SelectedItem; }

        set
        {
            m_SelectedItem = value;                
            NotifyPropertyChanged("SelectedProduct");
        }
    }

    private Product m_SelectedTab;
    public Product SelectedTab
    {
        get { return m_SelectedTab; }

        set
        {
            m_SelectedTab = value;
            NotifyPropertyChanged("SelectedTab");                
        }
    }

    private ICommand mUpdater;
    public ICommand ConnectCommand
    {
        get
        {
            if (mUpdater == null)
                mUpdater = new DelegateCommand(new Action(SaveExecuted), new Func<bool>(SaveCanExecute));

            return mUpdater;
        }
        set { mUpdater = value; }
    }

    public bool SaveCanExecute()
    {
        return true;
    }     

    public void SaveExecuted()
    {
        if (SelectedProduct.Connection_Status == "Disconnected" && SelectedProduct.Name == "Bavaria")
        {
            SelectedProduct.Connection_Status = "Connected";
            m_BoardTabs.Add(new Product() { TabOperation = "I2C" });         
            m_BoardTabs.Add(new Product() { TabOperation = "Voltage" });    
            m_BoardTabs.Add(new Product() { TabOperation = "Codec" });   
        }
    }
}

在Save Executed方法中,我试图在列表框中添加项目,但是当我运行应用程序并从listview中选择项目并按CONNECT Btn时,列表不会更新。我的模型类包含所有三个属性(名称,连接状态和TabOperation)

2 个答案:

答案 0 :(得分:3)

BoardTabsProducts必须是ObservableCollection<Product>。否则,WPF不会获知列表中的新项目,因此无法更新UI。

答案 1 :(得分:1)

检查ProductViewModel的属性。它实现了INotifyPropertyChanged,在Products,BoardTabs中,您不会通知更改。

  public List<Product> Products
        {
            get
            {
                return m_Products;
            }

            set
            {
                m_Products = value;
NotifyPropertyChanged("Products")
            }
        }

        public List<Product> BoardTabs
        {
            get
            {
                return m_BoardTabs;
            }

            set
            {
                m_BoardTabs = value;
NotifyPropertyChanged("BoardTabs")
            }
        }