我使用Caliburn.Micro库用MVVM(C#)和XAML编写了一个程序,我怎么能:
任何帮助都将不胜感激。
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 Command="{Binding HelloCommand}" Content="Hello ..." HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top" Width="75" ToolTip="Test" Background="White" Foreground="Black" Height="22" />
<Button Content="Add" Command="{Binding AddCommand}" HorizontalAlignment="Left" Margin="10,8,0,0" VerticalAlignment="Top" Width="75" ToolTip="Add item end of list" Background="White" Foreground="Black" Height="22"/>
<Button Content="Delete" Command="{Binding DeleteCommand}" HorizontalAlignment="Left" Margin="96,8,0,0" VerticalAlignment="Top" Width="75" ToolTip="Delete first item from list" Background="White" Foreground="Black" Height="22"/>
<Button Content="Update" Command="{Binding UpdateCommand}" HorizontalAlignment="Left" Margin="184,8,0,0" VerticalAlignment="Top" Width="75" ToolTip="Update first item from list" Background="White" Foreground="Black" Height="22"/>
<Button Content="GetSelectedItem" Command="{Binding GetSelectedItemCommand}" HorizontalAlignment="Left" Margin="271,8,0,0" VerticalAlignment="Top" Width="95" ToolTip="get selected item from list" Background="White" Foreground="Black" Height="22"/>
<Button Content="GetItem:" Command="{Binding GetItemXCommand}" HorizontalAlignment="Left" Margin="377,8,0,0" VerticalAlignment="Top" Width="75" ToolTip="get item x, from list" Background="White" Foreground="Black" Height="22"/>
<TextBox x:Name="ItemX" Text="0" HorizontalAlignment="Left" Height="20" Margin="455,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="70"/>
<TextBlock x:Name="OuputText" HorizontalAlignment="Left" Height="20" Margin="165,40,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="415" Foreground="#FFFF0909"/>
<Label Content="Output:" HorizontalAlignment="Left" Height="28" Margin="100,35,0,0" VerticalAlignment="Top" Width="55"/>
<ListView Grid.Column="1" x:Name="FileListView" SelectionMode="Extended" SelectedItem="{Binding SelectedItem}" SelectedIndex="{Binding SelectedIndex}" 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>
ViewModel代码: 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();
HelloCommand = new RelayCommand(DoHello, CanDoHello);
AddCommand = new RelayCommand(DoAdd, CanDoAdd);
DeleteCommand = new RelayCommand(DoDelete, CanDoDelete);
UpdateCommand = new RelayCommand(DoUpdate, CanDoUpdate);
GetSelectedItemCommand = new RelayCommand(DoGetSelectedItem, CanDoGetSelectedItem);
GetItemXCommand = new RelayCommand(DoGetGetItemX, CanDoGetItemX);
}
#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 < 100000; i++)
{
string strName = string.Format("{0}", i);
_Files.Add(new File() { FileStatus = "status", FileName = strName, FileSize = i, 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 OuputText textbox
private string _OuputText = "-";
public string OuputText
{
get
{
return _OuputText;
}
set
{
_OuputText = value;
OnPropertyChanged("OuputText");
}
}
#endregion
#region ItemX textbox
private string _ItemX = "0";
public string ItemX
{
get
{
return _ItemX;
}
set
{
int nItem = 0;
if (Int32.TryParse(value, out nItem))
{
if (nItem >= _Files.Count || nItem < 0)
return;
}
_ItemX = value;
OnPropertyChanged("ItemX");
}
}
#endregion
#region command button
#region Hello button
public RelayCommand HelloCommand { get; set; }
private bool CanDoHello(object obj)
{
return true;
}
private void DoHello(object obj)
{
System.Windows.MessageBox.Show("DoHello");
}
#endregion
#region Add button
public RelayCommand AddCommand { get; set; }
private bool CanDoAdd(object obj)
{
return true;
}
private void DoAdd(object obj)
{
string strName = string.Format("{0}", _Files.Count + 1);
_Files.Add(new File() { FileStatus = "status", FileName = strName, FileSize = _Files.Count, FileType = "type", FileEmailCount = "email_count", FileInfoCount = "info_count" });
}
#endregion
#region Delete button
public RelayCommand DeleteCommand { get; set; }
private bool CanDoDelete(object obj)
{
if (_Files.Count > 0)
return true;
return false;
}
private void DoDelete(object obj)
{
if (_Files.Count > 0)
{
if (SelectedItem != null)
_Files.Remove(SelectedItem);
else
_Files.RemoveAt(0);
}
}
#endregion
#region Update button
public RelayCommand UpdateCommand { get; set; }
private bool CanDoUpdate(object obj)
{
if (_Files.Count > 0)
return true;
return false;
}
private void DoUpdate(object obj)
{
// var vvv= SelectedItems;
if (_Files.Count > 0)
{
if (SelectedItem != null)
SelectedItem.FileName = "Updated ..."; // change FileName field
else
_Files[0].FileName = "Updated ...";
}
// List<File> FilesSelect = _Files.Where(p => p.FileID == _Files[0].FileID).ToList();
}
#endregion
#region GetSelectedItem button
public RelayCommand GetSelectedItemCommand { get; set; }
private bool CanDoGetSelectedItem(object obj)
{
if (_Files.Count > 0)
return true;
return false;
}
public File SelectedItem { get; set; }
public int SelectedIndex { get; set; }
private void DoGetSelectedItem(object obj)
{
if (_Files.Count > 0 && SelectedItem != null)
{
var selected_file = SelectedItem;
OuputText = string.Format("first selected index {0} -> [{1}]", SelectedIndex, selected_file.FileID);
}
}
#endregion
#region GetItemX button
public RelayCommand GetItemXCommand { get; set; }
private bool CanDoGetItemX(object obj)
{
if (_Files.Count > 0)
return true;
return false;
}
private void DoGetGetItemX(object obj)
{
int nItem = 0;
if (Int32.TryParse(ItemX, out nItem))
{
if (nItem < _Files.Count)
{
OuputText = string.Format("select index {0} -> [{1}]", nItem, _Files[nItem].FileID);
}
}
else
OuputText = "-";
}
#endregion
#endregion
}
}
ICommand代码: ViewModels \ RelayCommand.cs
using System;
using System.Windows.Input;
namespace ListBox_CaliburnMicro.ViewModels
{
public class RelayCommand : ICommand
{
#region field
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion
#region constructors
public RelayCommand(Action<object> execute) : this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region member
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion
#region raise
public void RaiseCanExecuteChanged()
{
/*if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);*/
}
#endregion
}
}
型号代码:型号\ File.cs
using System;
using System.ComponentModel;
namespace ListBox_CaliburnMicro.Model
{
public class File : INotifyPropertyChanged
{
private Guid _FileID;
public Guid FileID
{
get
{
return _FileID;
}
set
{
_FileID = value;
OnPropertyChanged("FileID");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string _FileStatus;
public string FileStatus
{
get
{
return _FileStatus;
}
set
{
_FileStatus = value;
OnPropertyChanged("FileStatus");
}
}
private string _FileName;
public string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
OnPropertyChanged("FileName");
}
}
public int _FileSize;
public int FileSize
{
get
{
return _FileSize;
}
set
{
_FileSize = value;
OnPropertyChanged("FileSize");
}
}
private string _FileType;
public string FileType
{
get
{
return _FileType;
}
set
{
_FileType = value;
OnPropertyChanged("FileType");
}
}
private string _FileEmailCount;
public string FileEmailCount
{
get
{
return _FileEmailCount;
}
set
{
_FileEmailCount = value;
OnPropertyChanged("FileEmailCount");
}
}
private string _FileInfoCount;
public string FileInfoCount
{
get
{
return _FileInfoCount;
}
set
{
_FileInfoCount = value;
OnPropertyChanged("FileInfoCount");
}
}
public File()
{
FileID = Guid.NewGuid();
}
public File(string s1 = "", string s2 = "", int s3 = 0, string s4 = "", string s5 = "", string s6 = "")
{
FileID = Guid.NewGuid();
FileStatus = s1;
FileName = s2;
FileSize = s3;
FileType = s4;
FileEmailCount = s5;
FileInfoCount = s6;
}
}
}
答案 0 :(得分:0)
您必须将这些事件连接到命令并在viewmodel中处理它们。
不使用Caliburn.Micro,但也应该有一个EventToCommand绑定的概念。我快速搜索显示以下链接。
https://caliburnmicro.codeplex.com/wikipage?title=Cheat%20Sheet
https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions
如果我的问题没有解决,请告诉我......
HTH