未能在列表框中添加按钮点击物品 [英] Failing to add items in listbox on button Click

查看:119
本文介绍了未能在列表框中添加按钮点击物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVVM一个新手。我有主窗口两格的内线,其中一个网格包含向左侧和其他网格右侧的列表框包含列表视图和2个按钮。

I am a newbie to MVVM. I have two grid's inside by main window where one grid contains a listbox towards left side and other grid on the right side contains list view and 2 buttons.

我能够将项目添加到ListView和甚至想出如何从列表视图中选择的项目。有一次,我选择列表视图中的项,然后单击一个按钮(连接),向左列表框shud得到与我从ViewModel类添加的项目更新。

I am able to add items to listview and even figure out how to get the selected item from list view. Once I select the item on list view and click a button(Connect), listbox towards left shud get updated with items which I have added from viewmodel class.

查看下图为朝我的左列表框:

<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>

查看下面显示的ListView走向右连同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" });   
        }
    }
}

在保存可执行的方法,我尝试添加在列表框中的项目,但是当我运行ListView和preSS CONNECT BTN的应用和选择项,列表不会被更新。我的模型类是完成与所有三个属性(名称,连接状态和TabOperation)

Inside the Save Executed method I am trying to add the items in listbox but when I run the application and select item from listview and press CONNECT Btn, the list does not get updated. My model class is complete with all three properties (Name, Connection Status and TabOperation)

推荐答案

BoardTabs 产品必须是一个的ObservableCollection&LT;产品与GT; 。否则WPF没有得到了解在列表中的新项目,因此无法更新UI

BoardTabs and Products need to be an ObservableCollection<Product>. Otherwise WPF doesn't get informed about new items in the lists and thus can't update the UI.

这篇关于未能在列表框中添加按钮点击物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆