在MVVM模式下,ListBox的Command绑定

本文介绍如何在MVVM模式下将ListBox的ItemTemplate中的按钮Click事件替换为DelegateCommand,通过在Model中添加命令属性并绑定到ButtonCommand,解决了找不到Command的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在学习MVVM模式,我想将ListBox的ItemTemplate中的按钮的Click事件改为用DelegateCommand实现,发现无从下手了:

View的内容:

<Grid x:Name="LayoutRoot">
        <ListBox x:Name="listBox1" Background="{x:Null}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemsSource="{Binding ButtonModelList}" VerticalAlignment="Center">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Content}" Click=“Button_Click”/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

ViewModel的内容:

 private List<ToolButton1Model> buttonModelList;

        public List<ToolButton1Model> ButtonModelList
        {
            get { return buttonModelList; }
            set { buttonModelList = value; this.RaisePropertyChanged("ButtonModelList"); }
        }


        public DelegateCommand CallEachCommand { get; set; }

        public ToolUserControlViewModel()
        {
        }

        public ToolUserControlViewModel(string name)
        {  
            CallEachCommand = new DelegateCommandnew Action(CallEachHandle));
            ButtonModelList = new List<ToolButton1Model>();
       ToolButton1Model bm = new ToolButton1Model()
{
Content = ”Button“};

            ButtonModelList.Add(bm);

     }

    private void CallEachHandle(){}

Model的内容

class ToolButton1Model : NotificationObject
    {
private object content;

        public object Content
        {
            get { return content; }
            set { content = value; this.RaisePropertyChanged("Content"); }
        }
    }

 

 

如果直接将Click="Button_Click"换成Command=”{Binding CallEachCommand}“会报错,说找不到CallEachCommand,因为ListBox已经指定了ItemsSource绑定的是ButtonModelList,在ButtonModelList这个集合中当然是不存在CallEachCommand的了。那该怎么解决呢?

    其实很简单,只要把要绑定的命令也当成是一个属性就行,在Model多加一个命令属性,修改后的代码如下:

修改后的Model:

class ToolButton1Model : NotificationObject
    {

        private object content;

        public object Content
        {
            get { return content; }
            set { content = value; this.RaisePropertyChanged("Content"); }
        }


        private DelegateCommand buttonCommand;

        public DelegateCommand ButtonCommand
        {
            get { return buttonCommand; }
            set { buttonCommand = value; this.RaisePropertyChanged("ButtonCommand"); }
        }


    }

修改后的Views:[注意,是要绑定Model中的ButtonCommand属性,而不是直接绑定CallEachCommand]

<Grid x:Name="LayoutRoot">
        <ListBox x:Name="listBox1" Background="{x:Null}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemsSource="{Binding ButtonModelList}" VerticalAlignment="Center">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Content}"  Command="{Binding ButtonCommand}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

在ViewModel中,只要将ToolButtonModel的Button1Command赋值为CallEachCommand即可。

...
...
ToolButton1Model bm = new ToolButton1Model(){Content = "string" , ButtonCommand=CallEachCommand}; ButtonModelList.Add(bm);
 ...


 

 

转载于:https://www.cnblogs.com/jojinshallar/articles/3118884.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值