C#绑定不适用于Property <t> </t>

时间:2013-04-04 04:37:14

标签: c# xaml mvvm

我目前正在尝试在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>));
    }
}

我的控件的ViewModel如下所示,并在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}"/>

我一直坚持这个问题的时间比我应该的时间长得多,并且相当肯定它只是一个简单的问题。有什么想法吗?

2 个答案:

答案 0 :(得分: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,但我不确定它是否有效。

答案 1 :(得分: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);
  }
}