无法使用TextBox。分配了Template的文本绑定进行管理

时间:2018-12-06 03:46:53

标签: wpf binding textbox controltemplate

我有模型合作伙伴:

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个按钮创建TextBox: 1-从某些列表中选择合作伙伴 2-用于打开带有合作伙伴详细信息的窗口 3-清除文本框

为此,我创建了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”

1 个答案:

答案 0 :(得分:0)

您的班级合作伙伴可能是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));
    }
}