0

我目前正在尝试在 C# 4 中遵循 MVVM,但在绑定工作时遇到了麻烦。

从底部开始,这是我的 Property 类,它应该处理为 XAML 绑定更改的属性:

namespace Visualizer.MVVM
{
    public class Property<T> : DependencyObject, INotifyPropertyChanged
    {
        //private T _Value;
        public T Value
        {
            get { return (T) GetValue(ValueProperty); }
            set
            {
                SetValue(ValueProperty, value);
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged()
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs("Value"));
            }
        }

        public Property(T val)
        {
            Value = val;
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(T), typeof(Property<T>));
    }
}

我的控件视图模型如下所示,并在 MainWindow.xaml.cs 中实例化:

public class CheckboxControlVM
{
    public Property<bool> IsChecked { get; set; }
    public Property<string> Name { get; set; }

    public CheckboxControlVM(bool isChecked, string name)
    {
        IsChecked = new Property<bool>(isChecked);
        Name = new Property<string>(name);
    }
}

该控件没有代码隐藏,因此这里是它的 XAML:

<UserControl x:Class="Visualizer.MVVM.Checkbox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Visualizer.MVVM"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel Orientation="Horizontal" x:Name="LayoutRoot">
        <CheckBox IsChecked="{Binding Path=IsChecked.Value, Mode=TwoWay}"/>
        <TextBlock Text="{Binding Path=Name.Value, Mode=OneWay}"/>
    </StackPanel>
</UserControl>

最后,这是 MainWindow.xaml 中的绑定:

<mvvm:Checkbox DataContext="{Binding Realtime}"/>

我被困在这个问题上的时间比我应该做的要长得多,并且相当肯定这只是一个简单的问题。有任何想法吗?

4

2 回答 2

1

我不太明白您想通过该物业设计实现什么目标。通常我不会在 WPF 中这样做,所以我不太确定这是否有帮助。

通常,我确实在 ViewModel 级别实现 INotifyPropertyChanged,而不是在 VM 拥有的属性中。例子:

public class ViewModel : INotifyPropertyChanged{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

其次,除非我制作 WPF 用户控件,否则我不会使用 DependencyProperty。所以我使用私有属性并使用属性名称触发 OnPropertyChanged。

private string _name;
public string Name{
  set{
    _name = value;
    OnPropertyChanged("Name");
  }
}

最后,在 XAML 中,我使用 UpdateSourceTrigger=PropertyChanged 的​​绑定。

<TextBlock Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

也许您可以尝试添加UpdateSourceTrigger=PropertyChanged绑定,但我不确定它是否会起作用。

于 2013-04-04T05:14:49.230 回答
0

尝试这个

public class CheckboxControlVM
  {
    bool _isChecked = false;
    string _name ;
  public Property<bool> IsChecked { get { return _isChecked} set { _isChecked=value;} }

  public Property<string> Name { get { return _name } set { _name =value;} }

  public CheckboxControlVM(bool isChecked, string name)
  {
    _isChecked = isChecked;
     _name = name;
    IsChecked = new Property<bool>(_isChecked);
    Name = new Property<string>(_name);
  }
}
于 2013-04-04T04:52:12.377 回答