绑定用户控件WPF

时间:2018-08-26 14:12:20

标签: c# wpf data-binding

这是我的代码。

我尝试将pair.Host和pair.Hosted(在后面的代码中)绑定到HostTB和HostedTB(在xaml中)。

我参与了进行此绑定的尝试。 希望能有所帮助!

这是我的用户控件:

using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace UI
{
   /// <summary>
   /// Interaction logic for PairViewUC.xaml
   /// </summary>
   public partial class PairViewUC : UserControl
   {
       private Pair pair;
       public Pair Pair { get => pair; set => pair = value; }

       public PairViewUC(Pair _pair)
       {
           InitializeComponent();
           this.HostTB.Text = _pair.Host;
           this.HostedTB.Text = _pair.Hosted;
           this.pair = _pair;
           this.DataContext = this;
       }
   }
}

这是Pair类:

using System.ComponentModel;
namespace BE
{
    public class Pair : INotifyPropertyChanged
    {
        #region Fields
        private string host;
        private string hosted;
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region Properties
        public string Host { get => host;
            set
            {
                host = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Host"));
                }
            }
        }

        public string Hosted
        {
            get => hosted;
            set
            {
                hosted = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Hosted"));
                }
            }
        }
        #endregion

        #region Constructors
        public Pair()
        {
            host = null;
            hosted = null;
        }

        public Pair(string host,string hosted)
        {
            this.host = host;
            this.hosted = hosted;
        }

        public Pair(Pair another)
        {
            this.host = another.host;
            this.hosted = another.hosted;
        }
        #endregion
    }
}

这是用户控件的Xaml:

<UserControl x:Class="UI.PairViewUC"
         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:UI"
         mc:Ignorable="d" 
         d:DesignHeight="50" d:DesignWidth="300">

    <Border Name="borderHost">
        <TextBlock Name="HostTB" Text="{Binding Path=Pair.Host}"/>
    </Border>

    <Border Name="borderHosted" >
        <TextBlock Name="HostedTB" Text="{Binding Path=Pair.Hosted}"/>
    </Border>

</Grid>

我试图问尽可能合适的方法,抱歉,如果它不完美,我是新来的..

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试一下

public PairViewUC(Pair _pair)
   {
       InitializeComponent();
       this.DataContext = this;
       this.pair = _pair;
   }

在用户控件中

<UserControl x:Class="UI.PairViewUC"
     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:UI"
     mc:Ignorable="d" 
     d:DesignHeight="50" d:DesignWidth="300">

<Border Name="borderHost">
    <TextBlock Name="HostTB" Text="{Binding Path=Pair.Host}"/>
</Border>

<Border Name="borderHosted" >
    <TextBlock Name="HostedTB" Text="{Binding Path=Pair.Hosted}"/>
</Border>

在配对类中,而不是在默认构造方法中为null,而是将string.empty添加到属性。