8,735
社区成员
发帖
与我相关
我的任务
分享
private sys_Department currentDeparment;
public sys_Department CurrentDeparment
{
get { return currentDeparment; }
set
{
if (!ReferenceEquals(currentDeparment, value))
{
currentDeparment = value;
this.RaisePropertyChanged(() => this.CurrentDeparment);
((DelegateCommand)DeleteDeptCommand).RaiseCanExecuteChanged();
}
}
}
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Path=AllStudents}">
<StackPanel>
<Button Command="{Binding MyCommand}" Content="Go" />
<ListBox x:Name="listStudents" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Age}" />
<!--<Button Command="{Binding MyCommand}" Content="Change Name" />-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
MainPage.xaml
<ListBox x:Name="listStudents" ItemsSource="{Binding Path=AllStudents}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Age}" />
<Button Command="{Binding MyCommand}" Content="Change Name" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
// Model
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
// StudentViewModel
public class StudentViewModel : NotificationObject
{
private Student _student;
public StudentViewModel(Student student)
{
this._student = student;
}
public string Name
{
get
{
return _student.Name;
}
set
{
if (_student.Name != value)
{
_student.Name = value;
RaisePropertyChanged(() => this.Name);
}
}
}
public int Age
{
get
{
return _student.Age;
}
set
{
if (_student.Age != value)
{
_student.Age = value;
RaisePropertyChanged(() => this.Age);
}
}
}
private DelegateCommand<object> _cmd;
public ICommand MyCommand
{
get
{
if (_cmd == null)
_cmd = new DelegateCommand<object>(this.Do, this.CanDo);
return _cmd;
}
}
void Do(object param)
{
Name = "new value";
}
bool CanDo(object param)
{
if (Age > 30)
return false;
else
return true;
}
}
// MainPageViewModel
public class MainPageViewModel
{
public ICollectionView AllStudents { get; set; }
public MainPageViewModel()
{
ObservableCollection<StudentViewModel> Students = new ObservableCollection<StudentViewModel>();
for (int i = 0; i < 100; i++)
{
Students.Add(new StudentViewModel(new Student { Name = "Student " + i, Age = 20 + i }));
}
AllStudents = new PagedCollectionView(Students);
}
}
<!--
如果DataGrid的DataContext绑定的是ObservableCollection,那么MyCommand的CanExecute逻辑不起作用。
换作绑定ICollectionView则没问题,我估计绑定CollectionViewSource也是可以的。
我觉得这里肯定有个说道,而且应该属于非常基础级的知识点。
说真的,别看是小问题,我感觉暴露出我很多知识盲点。
-->
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Path=AllStudents}">
<StackPanel>
<Button Command="{Binding MyCommand}" Content="Go" />
<ListBox x:Name="listStudents" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Age}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
public class Employee {
public bool CanBeLoved{
get{ return Age <=30; } // 老兄你也太狠了,0-30岁的通杀,30以上的不要
}
}
<Button Command="{Binding Path=MyCommand}" IsEnabled="{Binding Path=Employees.CurrentItem.CanBeLoved}" Content="I love you." />
<Button Command="{Binding Path=MyCommand}"
IsEnabled="{Binding SelectedItem.CanBeLoved,ElementName=listBox1}"
Content="I love you." />
<Button Command="{Binding Path=MyCommand}" CommandParameter="{Binding Path=Employees.CurrentItem}" Content="I love you." />
// ViewModel
public class EmployeeViewModel : NotificationObject
{
public ICollectionView Employees { get; private set; }
public ICommand MyCommand { get; private set; }
public Employee CurrentEmployee
{
get
{
return current;
}
set
{
if (value != current)
{
current = value;
RaisePropertyChanged("CurrentEmployee");
}
}
}
private readonly ObservableCollection<Employee> employees;
private Employee current;
public EmployeeViewModel()
{
employees = new ObservableCollection<Employee>();
for (int i = 0; i < 100; i++)
employees.Add(new Employee { Name = "Employee" + i, Age = 20 + i });
Employees = new PagedCollectionView(employees);
Employees.CurrentChanged += (s, e) =>
{
CurrentEmployee = Employees.CurrentItem as Employee;
};
CurrentEmployee = Employees.CurrentItem as Employee;
MyCommand = new DelegateCommand<object>(this.OnSubmit, this.CanSubmit);
}
private void OnSubmit(object param)
{
CurrentEmployee.Name = "change name";
}
public bool CanSubmit(object param)
{
return CurrentEmployee.Age <=30 ? true : false;
}
}
// 继承自Prism 4的NotificationObject
public class EmployeeViewModel : NotificationObject
{
private bool btnEnabled;
public bool BtnEnabled //与界面中Button的IsEnabled属性绑定
{
get
{
return this.btnEnabled;
}
set
{
if (value != btnEnabled)
{
this.btnEnabled = value;
this.RaisePropertyChanged(()=>this.BtnEnabled);
}
}
}
public ICollectionView Employees { get; private set; }
public EmployeeViewModel()
{
Employees = new PagedCollectionView(employees);
Employees.CurrentChanged += (s, e) =>
{
var CurrentEmployee = Employees.CurrentItem as Employee;
BtnEnabled = CurrentEmployee.Age <= 30 ? true : false;
};
}
}
<Button Command="{Binding MyCommand}" Content="I love you." IsEnabled="{Binding BtnEnabled}" />