作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何获得属性更改以注册复选框切换?我希望我的保存按钮在复选框被切换但无法触发属性更改时启用,因此我可以调用 RaiseCanExecuteChanged。我的复选框在 DataGrid 中。 IsGeneric 是 ProgramsSelected 的一个属性,我将该属性添加到我的 View 模型中以查看是否有帮助,但它没有。
XAML
<DataGrid ItemsSource="{Binding Programs}"
SelectedItem="{Binding ProgramSelected}"
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsGenericView, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Header="Is Generic" />
</DataGrid.Columns>
</DataGrid>
<Button Command="{Binding SaveProgramsCommand}" Content="Save Programs" />
namespace OPP.Models
{
using SimpleMvvmToolkit;
public class Program : ModelBase<Program>
{
private bool isGenericView;
public string Name { get; set; }
public string Value { get; set; }
public bool IsGenericView
{
get
{
return this.isGenericView;
}
set
{
this.isGenericView = value;
this.NotifyPropertyChanged(m => m.IsGenericView);
}
}
public bool ShowAtTop { get; set; }
}
}
public class ManageProgramsViewModel : ViewModelDetailBase<ManageProgramsViewModel, Program>
{
public ICommand SaveProgramsCommand
{
get
{
return this.saveProgramsDelegate
?? (this.saveProgramsDelegate = Helpers.DelegateCommandFactory.CreateCommand(this.SavePrograms, this.CheckCanSavePrograms));
}
}
}
public ObservableCollection<Program> Programs
{
get
{
return this.Programs;
}
private set
{
this.programs = value;
this.NotifyPropertyChanged(m => m.Programs);
}
}
public Program ProgramSelected
{
get
{
return this.programSelected;
}
private set
{
this.programSelected = value;
this.NotifyPropertyChanged(m => m.ProgramSelected);
}
}
public bool IsGenericView // Never hits the breakpoint when I check or uncheck my checkbox
{
get
{
return this.isGenericView;
}
set
{
this.isGenericView = value;
this.NotifyPropertyChanged(m => m.IsGenericView);
this.CanSavePrograms = !this.CanSavePrograms;
}
}
public bool CanSavePrograms
{
get
{
return this.canSavePrograms;
}
set
{
this.canSavePrograms = value;
Helpers.DelegateCommandFactory.RaiseCanExecuteChanged(this.SaveProgramsCommand);
}
}
最佳答案
在 XAML 中,您应该使用 DataTrigger
:
...
<Button Command="{Binding SaveProgramsCommand}" Content="Save Programs" >
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False" />
<Setter Property="Opacity" Value=".5" />
<Style.Triggers>
<DataTrigger Binding="{Binding ProgramSelected.IsGenericView}" Value="True">
<Setter Property="IsEnabled" Value="True" />
<Setter Property="Opacity" Value="1" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
...
Program
应该实现接口(interface)
INotifyPropertyChanged
:
public class Program : INotifyPropertyChanged
{
private bool _isGenericView;
public string Name { get; set; }
public string Value { get; set; }
public bool IsGenericView
{
get { return _isGenericView; }
set
{
_isGenericView = value;
OnPropertyChanged("IsGenericView");
}
}
public bool ShowAtTop { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
INotifyPropertyChanged
:
...
public Program ProgramSelected
{
get
{
return this.programSelected;
}
set
{
this.programSelected = value;
OnPropertyChanged("ProgramSelected");
}
}
...
关于wpf - 导致属性触发切换复选框 WPF DataGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146371/
我是一名优秀的程序员,十分优秀!