【问题标题】:WPF DataContext updated but UI not updatedWPF DataContext 已更新,但 UI 未更新
【发布时间】:2014-01-02 14:28:13
【问题描述】:

我有最简单的应用程序。我想在按下按钮时填充列表框。我使用绑定,窗口DataContext在一些操作后更新,但UI没有更新!

代码如下:

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="432,288.04,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <ListBox x:Name="urlsListBox" ItemsSource="{Binding Urls}" HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" VerticalAlignment="Top" Width="417"/>

</Grid>

MainWindow.xaml.cs

    namespace WpfApplication1
{

    public partial class MainWindow : Window
    {
        ViewModel model = new ViewModel();

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = model;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            model.GetUrls();
        }
    }
}

ViewModel.cs

    namespace WpfApplication1
{
    class ViewModel
    {
        private ObservableCollection<Url> Urls { get; set; }

        public ViewModel()
        {
            Urls = new ObservableCollection<Url>();
        }

        public void GetUrls()
        {
            for (int i = 0; i < 5; i++)
            {
                Urls.Add(new Url { link = i.ToString() });
            }
        }
    }

    public class Url
    {
        public string link { get; set; }
    }
}

【问题讨论】:

    标签: wpf user-interface binding datacontext


    【解决方案1】:

    您需要支持属性更改通知。使用 NuGet 包管理器来引用 MVVM Lite 项目,从 ViewModelBase 派生您的 ViewModel,然后将您的链接属性更改为:

    private string _link;
    {
        public string link
        {
            get {return this._link;}
            set {this._link=value; RaisePropertyChanged(() => this.link); }
        }
    }
    

    您还需要为您的 URLs 属性执行此操作,该属性需要公开才能进行绑定。我也知道这有点超出了问题的范围,但一般来说你不应该像这样使用 Click 处理程序,“正确”的方法是将 RelayCommand 添加到你的 ViewModel 并将你的按钮的 Command 属性绑定到那个。

    【讨论】:

    • ObservationCollection 已经有一个RaisePropertyChanged 事件和CollectionChanged 事件。
    • @Dom true,但这只会通知他添加到集合中和从集合中删除的项目。如果他想更改集合本身或任何项目中的字段,那么他需要为其他事物实现 INotifyPropertyChanged 行为。诚然,我的回答可能已经超出了他的具体问题,但在我的辩护中,我确实提到他需要公开集合属性才能绑定工作。
    【解决方案2】:

    问题源于ViewModel 类中的Urls 属性。需要将Urls设为public,否则MainWindow无法访问该属性:

    视图模型:

    namespace WpfApplication1
    {
        public class ViewModel 
        {
            //make this public otherwise MainWindow will not have access to it!
            public ObservableCollection<Url> Urls { get; set; }
    
            public ViewModel()
            {
                Urls = new ObservableCollection<Url>();
            }
    
            public void GetUrls()
            {
                for (int i = 0; i < 5; i++)
                {
                    Urls.Add(new Url { link = i.ToString() });
                }
            }
    }
    
        public class Url
        {
             public string link { get; set; }
        }
    
    }
    

    希望这对您有所帮助,如果您有任何问题,请告诉我!

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      相关资源
      最近更新 更多