gpt4 book ai didi

c# - 根据数据 View 值在 WPF 中启用按钮

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:00 25 4
gpt4 key购买 nike

我有一个绑定(bind)到名为 Activity 的类的数据 View 。在网格中有两列,分别是复选框列和文本框。

如果至少选中一个复选框并且其中一个文本框具有特定字符串,我想添加启用按钮的代码。我考虑过在 Activity 类中创建一个新属性并将其绑定(bind)到“isEnabled”属性,但不确定是否可行,因为没有可以触发 NotifyPropertyChanged 的​​设置方法。

有什么建议吗?

<DataGrid Name="dataGrid" HorizontalAlignment="Center"  CanUserAddRows="False" CanUserReorderColumns="False"  HeadersVisibility="Column" Margin="0,28,0,0" VerticalAlignment="Top" Height="262" AutoGenerateColumns="False" ItemsSource="{Binding Activities}" SelectedItem="{Binding SelectedActivity, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Enabled">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Enabled, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

最佳答案

根据我上面的评论,您可以利用 RelayCommand 执行此类操作,因为它有一个 CanExecute 函数,该函数会自行评估,因此不需要 NotifyPropertyChanged 事件以启用/禁用 Button

这里有一个完整的例子来展示这个工作原理,因为它可能有点难以解释

Xaml:

<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="315" Width="258" Name="UI">

<Grid DataContext="{Binding ElementName=UI}">
<DataGrid Name="dataGrid" CanUserAddRows="False" CanUserReorderColumns="False" HeadersVisibility="Column" AutoGenerateColumns="False" ItemsSource="{Binding Activities}" SelectedItem="{Binding SelectedActivity, Mode=TwoWay}" Margin="10,10,10,37" >
<DataGrid.Columns>
<DataGridTextColumn Header="Column1" Binding="{Binding Column1}" />
<DataGridTemplateColumn Header="Enabled">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Enabled, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button Command="{Binding MyButtonCommand}" Content="Is any checked &amp; is any 'Item6' " Margin="10,0,9,10" Height="22" VerticalAlignment="Bottom"/>
</Grid>
</Window>

代码:

namespace WpfApplication8
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<TestObject> myVar = new ObservableCollection<TestObject>();

public MainWindow()
{
MyButtonCommand = new RelayCommand(ExecuteButtonAction, CanButtonExecute);
InitializeComponent();

for (int i = 0; i < 100; i++)
{
Activities.Add(new TestObject { Column1 = "Item" + i, Enabled = false });
}
}

public ICommand MyButtonCommand { get; set; }

public ObservableCollection<TestObject> Activities
{
get { return myVar; }
set { myVar = value; }
}

private bool CanButtonExecute()
{
return Activities.Any(x => x.Enabled) && Activities.Any(x => x.Column1 == "Item2");
}

private void ExecuteButtonAction()
{
// button clicked
}
}


public class TestObject : INotifyPropertyChanged
{

private string _column1;
private bool _enabled;

public string Column1
{
get { return _column1; }
set { _column1 = value; NotifyPropertyChanged(); }
}

public bool Enabled
{
get { return _enabled; }
set { _enabled = value; NotifyPropertyChanged(); }
}


public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;

/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action execute)
: this(execute, null)
{
}

/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}

[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute();
}

public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
{
CommandManager.RequerySuggested += value;
}
}
remove
{
if (_canExecute != null)
{
CommandManager.RequerySuggested -= value;
}
}
}

public void Execute(object parameter)
{
_execute();
}
}

}

结果:(是否选中任何项目并且是任何项目“Item6”)

enter image description here enter image description here

关于c# - 根据数据 View 值在 WPF 中启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18393518/

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