Loading

wpf-mvvm开发学习2

开始重构 进程管理部分

部分代码

固定大小 设置标题

image-20220525151153965

重写ListBox

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="2*" />
                <ColumnDefinition Width="14*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="2*" />
            </Grid.RowDefinitions>
            <CheckBox
                      Grid.Column="0"
                      Padding="5"
                      IsChecked="{Binding Path=isChecked}" />
            <TextBlock
                       Grid.Column="2"
                       Padding="5"
                       FontSize="13"
                       Text="{Binding Path=pidName}" />
            <TextBlock
                       Grid.Column="1"
                       Padding="5"
                       Foreground="#808080"
                       Text="{Binding Path=pid}" />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

重写listbox触发器

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <!--  设置控件模板  -->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          TextBlock.Foreground="{TemplateBinding Foreground}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <!--  设置触发器  -->
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" Value="#66ccff" />
            </Trigger>
        </Style.Triggers>
    </Style>

</ListBox.ItemContainerStyle>

ViewModel

public class myProcessViewModel : ObservableObject
    {
        //构造函数
        public myProcessViewModel()
        {
            getProcessInfo();
            RefeshProcessCommand = new RelayCommand(RefeshProcessExecute);
            KillProcessCommand = new RelayCommand(KillProcessExecute);
        }
        private ObservableCollection<myProcess> _plist = new ObservableCollection<myProcess>();
        public ObservableCollection<myProcess> plist
        {
            get { return _plist; }
            set { SetProperty(ref _plist , value); }
        }
        
        
        private void getProcessInfo()
        {
            Process[] ppppp = Process.GetProcesses();
            Array.Sort(ppppp , (x , y) => x.ProcessName.CompareTo(y.ProcessName));
            foreach (Process p in ppppp)
            {   
                myProcess tItem = new myProcess();
                tItem.pid = p.Id;
                tItem.pidName = p.ProcessName;
                tItem.isChecked = false;
                try
                {
                    tItem.processPath = p.MainModule.FileName;
                }
                catch (Exception)
                {
                    tItem.processPath = "";
                }
                tItem.memorySize = p.WorkingSet64 / 1024 / 1024;
                
                plist.Add(tItem);
            }
        }
        public static void KillProcess(myProcess tItem)
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (p.Id == tItem.pid)
                {
                    p.Kill();
                    // p.WaitForExit(); // possibly with a timeout
                    // MessageBox.Show($"已杀掉{tItem.pidName}进程!!!");
                }
            }
        }
        
        
        public ICommand RefeshProcessCommand { get; }
        public ICommand KillProcessCommand { get; }

        
        void RefeshProcessExecute()
        {
            plist.Clear();
            getProcessInfo();
            MessageBox.Show("success"); 
        }
        void KillProcessExecute()
        {
            ObservableCollection<myProcess> tplist = new ObservableCollection<myProcess>();
            foreach (myProcess p in plist)
            {
                if (p.isChecked == true)
                {
                    KillProcess(p);
                    tplist.Add(p);
                }
            }
            foreach (myProcess p in tplist)
            {
                plist.Remove(p);
            }
            RefeshProcessExecute();
        }
    }

绑定DataContent

然后在MainWindow.xaml.cs中绑定DataContent

DrawerLeftProcessManager.DataContext = new myProcessViewModel();

image-20220525174432612

我将该ListBox的父控件的父控件的DataContext绑定为我的ViewModel

当ListBox找不到绑定项时会向上寻找DataContent

但是VS会报错没有绑定) 实际上是绑定成功了的

image-20220525175136019

或者在xaml中绑定

增加定义 xmlns:viewModel="clr-namespace:lltool_MVVM.ViewModel"

使用

            <hc:Drawer.DataContext>
                <viewModel:myProcessViewModel />
            </hc:Drawer.DataContext>

绑定按钮功能

<metro:MetroButton
                   Grid.Row="2"
                   Grid.Column="0"
                   Width="150"
                   Height="51"
                   Command="{Binding Path=RefeshProcessCommand}"
                   Content="刷新进程"
                   FontSize="20" />
<metro:MetroButton
                   Grid.Row="2"
                   Grid.Column="1"
                   Width="150"
                   Height="51"
                   Command="{Binding Path=KillProcessCommand}"
                   Content="终结进程"
                   FontSize="20" />

必须将这两个按钮和listbox的上层绑定DataContent

TODO:右键菜单

ContextMenu无论定义在.cs或.xaml文件中,都不继承父级的DataContext,所以如果要绑定父级的DataContext,直接DataContext=“{Binding}”是行不通的

posted @ 2022-05-26 10:37  FW_ltlly  阅读(117)  评论(0编辑  收藏  举报