简体   繁体   中英

binding user control WPF

This is my code.

I try to bind pair.Host and pair.Hosted (in the code behind) to HostTB and HostedTB (in the xaml).

I got involved in attempts to do this binding. Would appreciate help!

This is my user-control:

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;
       }
   }
}

And This the Pair class:

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
    }
}

And this is the Xaml of the user-control:

<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>

I tried to ask as proper as possible, sorry if it's not perfect, I'm new here ..

Thanks in advance!

Try this

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

in user control

<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>

and in pair class instead of giving null in default consturctor u add string.empty to property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM