wpf绑定到客户端不显示名称

时间:2015-10-20 13:24:23

标签: c# wpf

我创建了一个简单的解决方案,尝试创建一个“客户”,当应用程序运行时,客户名称会显示在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>

2 个答案:

答案 0 :(得分:6)

您需要在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>

答案 1 :(得分:1)

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

  1. 依赖属性here is the information
  2. 实现INotifyPropertyChange接口here is the information
  3. 只有这样,漫游将按您希望的方式工作。  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。看起来如何: enter image description here

    的问候,