无法使用 TextBox.Text 与分配的模板绑定进行管理

Can't manage with TextBox.Text binding with Template assigned

我有模特搭档:

public class Partner
{
    public Guid Id { get; set; }
    public string Title { get; set; }
    public string Comment { get; set; }

    public override string ToString()
    {
        return Title;
    }
}

用这个xaml查看:

<Window x:Class="WpfExtandedTextBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ModelViews="clr-namespace:WpfExtandedTextBox"
        d:DataContext="{d:DesignInstance ModelViews:ViewModel}"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">

    <Window.Resources>
        <ControlTemplate x:Key="entityTextBoxTemplate" TargetType="TextBox">
            <StackPanel Orientation="Horizontal">

                <TextBox MinWidth="200" Text="{Binding Partner.Title, Mode=TwoWay}"/>
                <TextBlock MaxWidth="0" Visibility="Hidden" Text="{Binding Partner.Id, Mode=TwoWay}"/>

                <Button x:Name="OpenPartnerList" Content="..." Click="OpenPartnerList_Click"/>
                <Button x:Name="OpenPartner" Content="O" Click="OpenPartner_Click"/>
                <Button x:Name="ClearPartner" Content="X" Click="ClearPartner_Click"/>

            </StackPanel>
        </ControlTemplate>
    </Window.Resources>

    <StackPanel Orientation="Vertical" >

        <TextBox x:Name="TextBoxSelectedPartner" Template="{StaticResource entityTextBoxTemplate}" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <!--<Button x:Name="ChoosePartner" Click="ChoosePartner_Click" Content="Choose partner"/>
        <DataGrid ItemsSource="{Binding Partners}" AutoGenerateColumns="True" />-->

    </StackPanel>

</Window>

并查看模型:

public class ViewModel : BaseViewModel
    {
        private List<Partner> partners;
        public List<Partner> Partners
        {
            get { return this.partners; }
        }

        private Partner partner;
        public Partner Partner
        {
            get { return this.partner; }
            set
            {
                this.partner = value;
                NotifyPropertyChanged();
            }
        }

        public ViewModel()
        {
            AppDbContext db = new AppDbContext();
            this.partners = db.Partners.ToList();
            db.Dispose();
        }
    }

我想创建带有 3 个按钮的文本框: 1 - 从一些列表中选择合作伙伴 2 - 使用合作伙伴的详细信息打开 window 3 - 用于清除 TextBox

为此,我创建了 ControlTemplate "entityTextBoxTemplate":TextBox 用于存储 Partner.Title,隐藏的 TextBlock 用于存储 Partner.Id。我假设从列表中选择合作伙伴后,TextBox 和 TextBlock 将分别填充 Title 和 Id,但它不起作用,我不知道为什么。谁能帮我解决这个问题?

更新: 合作伙伴在此代码中填充:

private void PartnerListView_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        viewModel.Partner = ((PartnerListView)sender).SelectedPartner;
    }

更新 2:

我的 BaseViewModel:

public class BaseViewModel
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

我固定的 BaseViewModel:

public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

我只是忘了指定实现的接口:“:INotifyPropertyChanged”

您的 class 合作伙伴可能是 INotifyPropertyChanged(我为您输入了 Title 的代码,这对于您的对象的其他参数也是相同的)

public class Partner : INotifyPropertyChanged
{
    private string title;
    public string Title
    {
       get { return this.title; }
       set
       {
          if (this.title != value)
          {
             this.title = value;
             this.NotifyPropertyChanged("Title");
          }
       }
    }

    public override string ToString()
    {
        return Title;
    }
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
       if (this.PropertyChanged != null)
       this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}