【问题标题】:Databinding not working with ViewModel数据绑定不适用于 ViewModel
【发布时间】:2011-12-27 11:01:23
【问题描述】:

无法获取任何数据以使用数据绑定,我有 INotify 事件,我有 xaml 对象上的绑定,但没有任何显示,如果我将标签上的内容更改为“某事”它可以工作,但没有显示在加载或点击我的按钮时

我的 Xaml 视图

<Grid>
    <StackPanel Name="stackpanel">
        <Label Content="{Binding Name}" />
        <Label Content="{Binding Length}" />
        <Label Content="{Binding Rating}" />
        <Button Content="Change text" Click="ButtonClick" />
    </StackPanel>
</Grid>

它的代码隐藏

public partial class Movie
{
    readonly MovieViewModel _movieViewModel;

    public Movie()
    {
        InitializeComponent();
        _movieViewModel = new MovieViewModel { Movie = { Name = "The Dark Knight", Length = 180, Rating = 88 } };
        stackpanel.DataContext = _movieViewModel;
    }

    private void ButtonClick(object sender, RoutedEventArgs e)
    {
        _movieViewModel.Movie.Name = "bad movie";
    }
}

视图模型

class MovieViewModel
{
    public MovieViewModel() : this(new Movie())
    {
    }

    public MovieViewModel(Movie movie)
    {
        Movie = movie;
    }

    public Movie Movie { get; set; } 
}

模型

class Movie : INotifyPropertyChanged
{
    public Movie()
    {}

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            NotifyPropertyChanged("Name");
        }
    }

    private int _length;
    public int Length
    {
        get { return _length; }
        set 
        { 
            _length = value;
            NotifyPropertyChanged("Length");
        }
    }

    private int _rating;
    public int Rating
    {
        get { return _rating; }

        set
        {
            if (_rating == value) return;
            _rating = value;
            NotifyPropertyChanged("_Rating");
        }
    }

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

【问题讨论】:

    标签: c# xaml data-binding


    【解决方案1】:

    您的绑定设置不正确,这就是没有显示任何内容的原因。

    只需仔细查看您的ViewModel,而不是绑定。您尝试绑定到名为 Name 的属性,但您的 MovieViewModel 没有公开任何具有该名称的属性。我很确定已经向您报告了绑定错误(查看输出窗口中的消息)。

    要使其正常工作,您需要在 ViewModel 中公开属性以匹配您尝试绑定到的属性(错误),或者更改 xaml 中的绑定以获得正确的路径:

    <Label Content="{Binding Movie.Name}" />
    <Label Content="{Binding Movie.Length}" />
    <Label Content="{Binding Movie.Rating}" />
    

    这应该能让你继续前进。

    此外 - 如果您计划更改分配给 Movie 属性的 Movie 对象,您可能还希望在您的 MovieViewModel 类上实现 INotifyPropertyChanged。只要您只更改已分配给 MovieViewModelMovie 对象的属性,一切都会好起来的,但是如果您尝试更改分配给此属性的实际对象,则不会生成更改通知并且您的 UI 将停止工作正确。

    此外 - 我注意到您公开了您的 NotifyPorpertyChanged 方法 - 我不建议这样做,因为现在任何人都可以触发此事件。通常的方法是使此类方法成为私有或受保护的,具体取决于您是否要提供从继承类触发事件的方法(这很可能是在PropertyChanged 事件的情况下)。

    【讨论】:

      【解决方案2】:

      我认为你有一个打字错误

      NotifyPropertyChanged("_Rating");
      

      应该是

       NotifyPropertyChanged("Rating");
      

      【讨论】:

        【解决方案3】:

        我建议你使用 Texblock,而不是使用 Label。试试下面的代码

        _movieViewModel = new MovieViewModel 
                              { Movie = { Name = "The Dark Knight", Length = 180, Rating = 88 } };
         this.DataContext = _movieViewModel;
        

        如下文本块

        <StackPanel Name="stackpanel">
            <TextBlock Name="textBlock1" Text="{Binding Path=Name}"/>
            <TextBlock Name="textBlock2" Text="{Binding Path=Length}"/>
            <Button Content="Change text" Click="ButtonClick" />
        </StackPanel>
        

        【讨论】:

          猜你喜欢
          • 2017-07-13
          • 1970-01-01
          • 2017-02-28
          • 2013-02-24
          • 1970-01-01
          • 2021-12-25
          • 2015-09-22
          • 2017-05-22
          • 2018-03-24
          相关资源
          最近更新 更多