【问题标题】:wpf binding to client is not displaying namewpf绑定到客户端不显示名称
【发布时间】:2015-10-20 22:19:23
【问题描述】:

我创建了一个简单的解决方案,试图创建一个“客户”,当应用程序运行时,客户名称会出现在 UI 的文本框中。为什么绑定属性并设置datacontext后似乎不显示?

MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApplication1.Model;

namespace WpfApplication1
{
    class MainWindowViewModel
    {
        private Customer client = new Customer();

        public MainWindowViewModel()
        {
            client.Name = "Greg Johnson";
            client.Friends = new ObservableCollection<string>() { "Leslie", "Mitch" };
        }
    }
}

XAML

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="300" Width="305">

    <Window.DataContext>
        <viewModel:MainWindowViewModel />
    </Window.DataContext>

    <Grid>
        <TextBox Text="{Binding Name}"/>
    </Grid>
</Window>

【问题讨论】:

  • 因为NameCustomer 的属性,而不是MainWindowViewModel
  • 那么我会修改什么来让它显示?

标签: c# wpf


【解决方案1】:

您需要在 View 模型中添加 Client 的属性。然后在绑定中更改为Client.Name。参考下面的代码。

class MainWindowViewModel
{
    public Customer Client { get; set; }

    public MainWindowViewModel()
    {
        Client = new Customer();
        Client.Name = "Greg Johnson";
        Client.Friends = new ObservableCollection<string>() { "Leslie", "Mitch" };
    }
}

<Window.DataContext>
    <viewModel:MainWindowViewModel />
</Window.DataContext>

<Grid>
    <TextBox Text="{Binding Client.Name}"/>
</Grid>

【讨论】:

    【解决方案2】:

    这里有一个概念错误。绑定到 xaml 代码的每个属性必须是:

    1. 依赖属性here is the information
    2. 实现 INotifyPropertyChange 接口 here is the information

    只有这样,binnding 才能按您希望的方式工作。 1.Xaml:

    <Window x:Class="NirHelpingOvalButton.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:nirHelpingOvalButton="clr-namespace:NirHelpingOvalButton"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <nirHelpingOvalButton:MainWindowViewModel />
    </Window.DataContext>
    
    <Grid>
        <TextBox Text="{Binding Client.Name}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid></Window>
    

    2。查看模型:

    public class MainWindowViewModel:BaseObservableObject
    {
        public MainWindowViewModel()
        {
            Client = new Customer
            {
                Name = "Steve",
                Friends = new ObservableCollection<string>(new List<string> {"John", "Alex", "Yakov"})
            };
        }
    
        private Customer _customer;
    
        public Customer Client
        {
            get { return _customer; }
            set
            {
                _customer = value;
                OnPropertyChanged();
            }
        }
    }
    

    3。型号代码:

    public class Customer:BaseObservableObject
    {
        private string _name;
    
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged();
            }
        }
    
        public ObservableCollection<string> Friends { get; set; }
    }
    

    4。 BaseObservableObject 代码:

    public class BaseObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    
        protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
        {
            var propName = ((MemberExpression)raiser.Body).Member.Name;
            OnPropertyChanged(propName);
        }
    
        protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null)
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                OnPropertyChanged(name);
                return true;
            }
            return false;
        }
    }
    

    5。它的样子:

    问候,

    【讨论】:

    • 这很棒。感谢您的帮助。这解释得很好,也很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2020-03-14
    • 1970-01-01
    相关资源
    最近更新 更多