我寫了一個簡單的程序與MVVM(C#)和XAML使用Caliburn.Micro庫, 但有些動作沒有工作:我如何更新ListView中的項目與Caliburn.Micro在C#中?
- 更新項目內容
- 獲得所選項目
- 得到具體項目
- 移和下移記錄
- 啓用或禁用按鈕時添加按鈕,點擊
任何幫助,將不勝感激。
我的GUI代碼:
// Views\MainView.xaml
<Window x:Class="ListBox_CaliburnMicro.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:vm="clr-namespace:ListBox_CaliburnMicro"
WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="300" Width="600">
<Grid>
<Grid.DataContext>
<vm:MainViewModel/>
</Grid.DataContext>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="153*"/>
</Grid.ColumnDefinitions>
<Button Content="Add" Command="{Binding AddCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="10,37,0,0" VerticalAlignment="Top" Width="75" ToolTip="Add item end of list" Background="White" Foreground="Black" Height="22"/>
<Button Content="Delete" Command="{Binding DeleteCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="96,37,0,0" VerticalAlignment="Top" Width="75" ToolTip="Delete first item from list" Background="White" Foreground="Black" Height="22"/>
<Button Content="Update" Command="{Binding UpdateCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="184,37,0,0" VerticalAlignment="Top" Width="75" ToolTip="Update first item from list" Background="White" Foreground="Black" Height="22"/>
<Button Content="GetSelectedItem" Command="{Binding GetSelectedItemCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="271,37,0,0" VerticalAlignment="Top" Width="95" ToolTip="get selected item from list" Background="White" Foreground="Black" Height="22"/>
<Button Content="GetItem:" Command="{Binding GetItemXCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="377,37,0,0" VerticalAlignment="Top" Width="75" ToolTip="get item x, from list" Background="White" Foreground="Black" Height="22"/>
<TextBox Grid.Column="1" x:Name="ItemX" Text="0" HorizontalAlignment="Left" Height="24" Margin="457,35,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="70"/>
<ListView Grid.Column="1" x:Name="FileListView" ItemsSource="{Binding Path=Files}" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="10,65,10,10" Foreground="Black" Background="#FFE6EEF7">
<ListView.View>
<GridView>
<GridViewColumn Header="Status" Width="Auto"
DisplayMemberBinding="{Binding FileStatus}"/>
<GridViewColumn Header="Name" Width="Auto"
DisplayMemberBinding="{Binding FileName}"/>
<GridViewColumn Header="Size" Width="Auto"
DisplayMemberBinding="{Binding FileSize}"/>
<GridViewColumn Header="System Type" Width="Auto"
DisplayMemberBinding="{Binding FileType}"/>
<GridViewColumn Header="Email Count" Width="Auto"
DisplayMemberBinding="{Binding FileEmailCount}"/>
<GridViewColumn Header="Info Count" Width="Auto"
DisplayMemberBinding="{Binding FileInfoCount}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
我的模式是:
// Model\File.cs
using System;
namespace ListBox_CaliburnMicro.Model
{
public class File
{
private Guid _FileID;
public Guid FileID
{
get
{
return _FileID;
}
set
{
_FileID = value;
}
}
public string FileStatus { get; set; }
public string FileName { get; set; }
public string FileSize { get; set; }
public string FileType { get; set; }
public string FileEmailCount { get; set; }
public string FileInfoCount { get; set; }
public File()
{
FileID = Guid.NewGuid();
}
public File(string s1 = "", string s2 = "", string s3 = "", string s4 = "", string s5 = "", string s6 = "")
{
FileID = Guid.NewGuid();
FileStatus = s1;
FileName = s2;
FileSize = s3;
FileType = s4;
FileEmailCount = s5;
FileInfoCount = s6;
}
}
}
我的命令處理程序代碼:
// ViewModels\CommandHandler.cs
using System;
using System.Windows.Input;
namespace ListBox_CaliburnMicro.ViewModels
{
public class CommandHandler : ICommand
{
private System.Action _action;
private bool _canExecute;
public CommandHandler(System.Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
}
}
我的視圖模型:
// ViewModels\MainViewModel.cs
using Caliburn.Micro;
using ListBox_CaliburnMicro.Model;
using ListBox_CaliburnMicro.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace ListBox_CaliburnMicro
{
public class MainViewModel : Screen, INotifyPropertyChanged
{
public MainViewModel()
{
FillDataFile();
_canAddCommandExecute = true;
_canDeleteCommandExecute = false;
_canUpdateCommandExecute = false;
_canGetSelectedItemCommandExecute = false;
_canGetItemXCommandExecute = false;
}
#region File listView
private ObservableCollection<File> _Files;
public ObservableCollection<File> Files
{
get { return _Files; }
set
{
_Files = value;
OnPropertyChanged("Files");
}
}
private void FillDataFile()
{
_Files = new ObservableCollection<File>();
for (int i = 0; i < 0; i++)
{
_Files.Add(new File() { FileStatus = "status", FileName = "name", FileSize = "size", FileType = "type", FileEmailCount = "email_count", FileInfoCount = "info_count" });
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region ItemX textbox
private string _ItemX = "0";
public string ItemX
{
get
{
return _ItemX;
}
set
{
_ItemX = value;
}
}
#endregion
#region command button
#region Add button with Command
private ICommand _AddCommand;
public ICommand AddCommand
{
get
{
return _AddCommand ?? (_AddCommand = new CommandHandler(() => AddAction(), _canAddCommandExecute));
}
}
static int nIndex = 0;
private bool _canAddCommandExecute;
public void AddAction()
{
string strName = string.Format("{0}", nIndex++);
_Files.Add(new File() { FileStatus = "status", FileName = strName, FileSize = "size", FileType = "type", FileEmailCount = "email_count", FileInfoCount = "info_count" });
// enabled action buttons,
// !!! but not worded !!!
_canAddCommandExecute = true;
_canDeleteCommandExecute = true;
_canUpdateCommandExecute = true;
_canGetSelectedItemCommandExecute = true;
_canGetItemXCommandExecute = true;
}
#endregion
#region Delete button with Command
private ICommand _DeleteCommand;
public ICommand DeleteCommand
{
get
{
return _DeleteCommand ?? (_DeleteCommand = new CommandHandler(() => DeleteAction(), _canDeleteCommandExecute));
}
}
private bool _canDeleteCommandExecute;
public void DeleteAction()
{
if (_Files.Count > 0)
_Files.RemoveAt(0);
}
#endregion
#region Update button with Command
private ICommand _UpdateCommand;
public ICommand UpdateCommand
{
get
{
return _UpdateCommand ?? (_UpdateCommand = new CommandHandler(() => UpdateAction(), _canUpdateCommandExecute));
}
}
private bool _canUpdateCommandExecute;
public void UpdateAction()
{
if (_Files.Count > 0)
{
// update FileName field
_Files[0].FileName = "+"; // !!! but didn't work !!!
// List<File> FilesSelect = _Files.Where(p => p.FileID == _Files[0].FileID).ToList();
}
}
#endregion
#region GetSelectedItem button with Command
private ICommand _GetSelectedItemCommand;
public ICommand GetSelectedItemCommand
{
get
{
return _GetSelectedItemCommand ?? (_GetSelectedItemCommand = new CommandHandler(() => GetSelectedItemAction(), _canGetSelectedItemCommandExecute));
}
}
private bool _canGetSelectedItemCommandExecute;
public void GetSelectedItemAction()
{
if (_Files.Count > 0)
{
// ... get selected item in ListView
}
}
#endregion
#region GetItemX button with Command
private ICommand _GetItemXCommand;
public ICommand GetItemXCommand
{
get
{
return _GetItemXCommand ?? (_GetItemXCommand = new CommandHandler(() => GetItemXAction(), _canGetItemXCommandExecute));
}
}
private bool _canGetItemXCommandExecute;
public void GetItemXAction()
{
if (_Files.Count > 0)
{
// ... get item 'X' that enter in TextBox control
}
}
#endregion
#endregion
}
}
你是什麼意思 「上下記錄」? – StepUp
@StepUp對不起,我的意思是MoveUp和Movedown。 – mohammadmot
最好提出關於如何「獲得特定項目」以及如何「上移和下移記錄」的新問題,因爲它沒有足夠的關於你想要的信息。 – StepUp