我绑定了一个布尔列表,显示了一个复选框列表。
我现在想更新一个Label,该标签显示选中了多少个复选框。每当选中任何复选框时,它都必须更新。
我正在努力工作,因为我不知道将复选框绑定到什么,以便更新标签所绑定的字符串。
为此,我认为我需要将每个复选框绑定到一个正在运行的命令。 但是,我不知道如何更新标签。 如果有更简单的方法(我敢肯定有这样做),请告诉我。
public class CheckBoxBoolean : INotifyPropertyChanged
{
public bool CheckBoxChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
OnPropertyChanged("CheckBoxChecked");
}
}
public CheckBoxBoolean(bool isChecked)
{
_isChecked = isChecked;
}
private bool _isChecked = false;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
<StackPanel Grid.Row="0" Grid.Column="1">
<ItemsControl ItemsSource="{Binding Path=Patterns1}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=CheckBoxChecked, Mode=TwoWay}" Command="{Binding Path=DataContext.CheckBoxChanged, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
private ICommand _checkBoxChanged;
public string Percentage1 { get; set; }
public ICommand CheckBoxChanged
{
get
{
if(_checkBoxChanged == null)
{
_checkBoxChanged = new CheckBoxChanging(Patterns1);
}
return _checkBoxChanged;
}
}
public class CheckBoxChanging : ICommand
{
public event EventHandler CanExecuteChanged;
private readonly ObservableCollection<CheckBoxBoolean> _Patterns;
public CheckBoxChanging(ObservableCollection<CheckBoxBoolean> items)
{
_Patterns = items;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
int i = 0;
foreach (CheckBoxBoolean chk in _Patterns)
{
if (chk.CheckBoxChecked)
i++;
}
Debug.WriteLine("UPD - Pattern 1 % = " + i / 16d);
}
}
答案 0 :(得分:1)
您好,我只是为您的情况写了一个示例,请看一下,我认为这会有所帮助
Xaml
<StackPanel Grid.Row="0" Grid.Column="1">
<ItemsControl ItemsSource="{Binding Path=CBItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=CheckBoxChecked, Mode=TwoWay}" Command="{Binding Path=DataContext.CheckBoxChanged, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Label Content="{Binding LabelContent}"></Label>
</StackPanel>
Ctro(设置数据上下文)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MainWindowVM DC = new MainWindowVM();
DC.Init();
this.DataContext = DC;
}
}
中继命令和模型类
class MainWindowVM : INotifyPropertyChanged
{
public MainWindowVM()
{
CheckBoxChanged = new RelayCommand(CheckBoxChangedMethod);
}
private string labelContent="Not Yet Checked";
public string LabelContent
{
get { return labelContent; }
set { labelContent = value; OnPropertyChanged(new PropertyChangedEventArgs("LabelContent")); }
}
public void Init()
{
try
{
CBItems = new ObservableCollection<ex>();
for (int i = 985; i <= 1030; i++)
CBItems.Add(new ex { CheckBoxChecked = true });
}
catch (Exception ex)
{
}
}
public ICommand CheckBoxChanged { get; set; }
private ObservableCollection<ex> _CBItems;
public ObservableCollection<ex> CBItems
{
get { return _CBItems; }
set
{
_CBItems = value;
OnPropertyChanged(new PropertyChangedEventArgs("CBItems"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
public void CheckBoxChangedMethod(object obj)
{
LabelContent = "You have Clicked the checkbox";
}
}
public class RelayCommand : ICommand
{
private Action<object> execute;
private Func<object, bool> canExecute;
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove { CommandManager.RequerySuggested -= value; }
}
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
//return this.canExecute == null || this.canExecute(parameter);
return true;
}
public void Execute(object parameter)
{
this.execute(parameter);
}
public RelayCommand(Action<object> execute)
{
this.execute = execute;
}
}
public class ex
{
private bool _checkBoxChecked;
public bool CheckBoxChecked
{
get { return _checkBoxChecked; }
set { _checkBoxChecked = value; }
}
}