WPF MVVM COMMOND 傳參

一、直接綁定(對於有事件的控件可以通過直接綁定的方式)

1、view

<hc:SideMenuItem Header="接談中" Cursor="Hand" Command="{Binding AddTabItemCommand}" CommandParameter="PDjtList.xaml">
                        <hc:SideMenuItem.Icon>
                            <Image Source="/Images/icons/jtz.png" Width="24" Height="24"/>
                        </hc:SideMenuItem.Icon>
                    </hc:SideMenuItem>

2、viewmodel

/// <summary>
        /// 命令:傳遞參數
        /// </summary>
        public RelayCommand<string> AddTabItemCommand =>
            new Lazy<RelayCommand<string>>(() =>
                new RelayCommand<string>(AddTabItem)).Value;


        /// <summary>
        /// 添加新頁面
        /// </summary>
        /// <param name="param"></param>
        private void AddTabItem(string param)
        {
            string test = param;


        }

二、EventTrigger綁定(對於通過datatemp遍歷的控件沒有事件可以綁定的情況,如listbox)

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<ListBox Name="sideMenu" SelectedIndex="{Binding MenuSelectedIndex}" ItemsSource="{Binding MenuList}" BorderThickness="0" SelectionMode="Single">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <DockPanel Height="40">
                                <Image Width="20" Height="20" Source="{Binding Icons}"/>
                                <TextBlock Margin="10,12,0,0" FontSize="14" Text="{Binding Title}"/>
                                <TextBox x:Name="param" Visibility="Hidden" Text="{Binding Url}"/>
                            </DockPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <i:Interaction.Triggers>
                        <i:EventTrigger  EventName="SelectionChanged">
                            <command:EventToCommand Command="{Binding ElementName=sideMenu, Path=DataContext.SelectMenuCommand}" PassEventArgsToCommand="True" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </ListBox>
/// <summary>
        /// 菜單選擇事件
        /// </summary>
        public RelayCommand<SelectionChangedEventArgs> SelectMenuCommand =>
            new Lazy<RelayCommand<SelectionChangedEventArgs>>(() =>
                new RelayCommand<SelectionChangedEventArgs>(SelectMenu)).Value;

        /// <summary>
        /// 選中菜單打開頁面
        /// </summary>
        private void SelectMenu(SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0) return;
            if (e.AddedItems[0] is MenuInfo item)
            {
                if (Equals(_selectedMenu, item)) return;

                _selectedMenu = item;
                DataList.Add(new TabControlModel
                {
                    Header = item.Title,
                    Url = item.Url
                });
                TabSelectedIndex = DataList.ToArray().Length - 1;

            }
        }

三、Image點擊事件

<Image x:Name="img_verifycode" Height="40" Margin="0,10,30,0" Source="{Binding VerifyCode}"></Image>
                                                <i:Interaction.Triggers>
                                                    <i:EventTrigger  EventName="MouseLeftButtonDown">
                                                        <command:EventToCommand Command="{Binding ElementName=img_verifycode, Path=DataContext.SwitchVerifyCodeCommand}" />
                                                    </i:EventTrigger>
                                                </i:Interaction.Triggers>

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章