【问题标题】:Failing to add items in listbox on button Click无法在按钮单击的列表框中添加项目
【发布时间】:2012-09-09 12:13:07
【问题描述】:

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

我能够将项目添加到列表视图,甚至可以弄清楚如何从列表视图中获取所选项目。一旦我在列表视图中选择了项目并单击按钮(连接),左侧的列表框就会更新为我从 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 方法中,我试图在列表框中添加项目,但是当我运行应用程序并从列表视图中选择项目并按 CONNECT Btn 时,列表不会更新。我的模型类包含所有三个属性(名称、连接状态和 TabOperation)

【问题讨论】:

    标签: wpf listview mvvm listboxitem


    【解决方案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")
                }
            }
    

    【讨论】:

    【解决方案2】:

    BoardTabsProducts 必须是 ObservableCollection&lt;Product&gt;。否则 WPF 不会收到有关列表中新项目的通知,因此无法更新 UI。

    【讨论】:

    • 非常感谢丹尼尔。上帝保佑你:)
    • 当项目插入到 ListBox 中时,我单击 12C 并在 selectTab 的帮助下,我希望将右侧的 Grid 替换为 I2C 的 UI。请帮忙
    • @user1084880:这似乎是一个新问题。请张贴这样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多