c# - 使用 MVVM 如何从许多可视组件或选项更新标签?

标签 c# .net xaml mvvm

我试图了解每次使用 WPF (XAML) 修改窗口上的选项时如何调用 Resfresh() 或 Work() 方法。我已经问过这个问题了,但我还不够清楚。所以我会用一个更好的例子再次询问。

我想知道如何更新许多可视组件的标签。假设我们有 10 个带有标签 0 到 9 的复选框,如果它们被选中,我想对它们进行求和。

在经典 Winform 中,我将创建一个事件处理程序 OnClick() 并在每个 CheckBox 状态更改时调用该事件。 OnClick 调用 Refresh() 全局方法。刷新评估每个复选框是否被选中,并根据需要对它们求和。在 Refresh() 方法的最后,我将 Label Text 属性设置为我的总和。

如何使用 XAML 和数据绑定(bind)来做到这一点?

<CheckBox Content="0" Name="checkBox0" ... IsChecked="{Binding Number0}" />
<CheckBox Content="1" Name="checkBox1" ... IsChecked="{Binding Number1}" />
<CheckBox Content="2" Name="checkBox2" ... IsChecked="{Binding Number2}" />
<CheckBox Content="3" Name="checkBox3" ... IsChecked="{Binding Number3}" />
<CheckBox Content="4" Name="checkBox4" ... IsChecked="{Binding Number4}" />
...
<Label Name="label1" ... Content="{Binding Sum}"/>

在我的 ViewModel 中,每个复选框都有一个数据绑定(bind)属性,还有一个用于 Sum

private bool number0;
public bool Number0
{
    get { return number0; }
    set
    {
        number0 = value;
        NotifyPropertyChanged("Number0");
        // Should I notify something else here or call a refresh method?
        // I would like to create something like a global NotifyPropertyChanged("Number")
        // But how can I handle "Number" ???
    }
}

// Same for numer 1 to 9 ...

private bool sum;
public bool Sum
{
    get { return sum; }
    set
    {
        sum = value;
        NotifyPropertyChanged("Sum");
    }
}

private void Refresh() // or Work()
{ 
    int result = 0;
    if (Number0)
        result = result + 0; // Could be more complex that just addition
    if (Number1)
        result = result + 1; // Could be more complex that just addition
    // Same until 9 ...
    Sum = result.ToString(); 
}

我的问题是我应该如何以及何时调用此 Refresh 方法?

最佳答案

您的设计中有几个错误。这是我要做的:

而不是 Number0...9 ,做一个BindingList<bool> Numbers

然后在 XAML 中显示复选框,如下所示:

<ItemsControl ItemsSource="{Binding Numbers}">
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <CheckBox IsChecked="{Binding} Content="Name the checkbox here" />
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

<Label Content="{Binding Numbers, Converter={StaticResource NumbersToSumConverter}}" />

NumbersToSumConverter IValueConverter ,如:

public NumbersToSumConverter : IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
      var numbers = value as BindingList<bool>();
      //Do your sum here.
   }

   public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
      throw new NotImplementedException();
   }
}

如果您需要在 BindingList<T> 中存储的不仅仅是一个 bool 值,那么这是在 MVVM 中如何完成的示例。 ,
创建一个实现 INotifyPropertyChanged 的新类,并根据需要添加任意数量的属性(确保在其 setter 中引发 PropertyChanged )。
然后使用它作为 BindingList<T> 的类型.

希望这有帮助,

巴巴。

关于c# - 使用 MVVM 如何从许多可视组件或选项更新标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11537284/

相关文章:

c# - 动态修改 LINQ to SQL Select 语句的列

c# - 我可以更好地优化这个并发吗?

c# - DependencyProperty 不适用于 CustomControl

silverlight - 在 XAML 中按下 "ENTER"键时调用命令

c# - 将秒转换为 hhh :mm:ss in a chart

c# - 实时优化传感器数据,通过省略条目来减少日志文件的大小

c# - 远程调用永不返回

c# - WCF 客户端 : Forcing Global Namespaces

.net - Kestrel MaxRequestBodySize 上传文件超限

c# - 按钮没有正确的签名xamarin