обязательный пользовательский контроль WPF - PullRequest
0 голосов
/ 26 августа 2018

Это мой код.

Я пытаюсь связать 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;
       }
   }
}

А это пара класса:

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 голосов
/ 26 августа 2018

Попробуйте это

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>

и в парном классе вместо того, чтобы давать нулевое значение в констуркторе по умолчанию и добавьте свойство string.empty в свойство.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...