gpt4 book ai didi

wpf - 导致属性触发切换复选框 WPF DataGrid

转载 作者:行者123 更新时间:2023-12-03 10:43:31 24 4
gpt4 key购买 nike

如何获得属性更改以注册复选框切换?我希望我的保存按钮在复选框被切换但无法触发属性更改时启用,因此我可以调用 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; }
}
}

View 模型
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>
...

类(class) 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));
}
}
}

你的 ViewModel 也应该实现接口(interface) INotifyPropertyChanged :
...
public Program ProgramSelected
{
get
{
return this.programSelected;
}
set
{
this.programSelected = value;
OnPropertyChanged("ProgramSelected");
}
}
...

关于wpf - 导致属性触发切换复选框 WPF DataGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com