Search code examples
wpfbindingtextboxcontroltemplate

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


I have model Partner:

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

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

view with this 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>

and view model:

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

I'd like to create TextBox with 3 buttons: 1 - for choosing Partner from some list 2 - for opening window with Partner's details 3 - for clearing TextBox

For this purpose I've created ControlTemplate "entityTextBoxTemplate": TextBox is for storing Partner.Title and hidden TextBlock is for storing Partner.Id. I assume that after choosing a Partner from list TextBox and TextBlock will be filled with Title and Id respectively, but it doesn't work and I don't know why. Can anybody help me to solve this issue?

Updated: Partner is populated in this code:

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

Updated 2:

My BaseViewModel:

public class BaseViewModel
    {
        public event PropertyChangedEventHandler PropertyChanged;

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

My fixed BaseViewModel:

public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

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

I just forgot to specify implemented interface: ": INotifyPropertyChanged"


Solution

  • your class Partner may be INotifyPropertyChanged (I put you code for Title, and this is the same for other parameters of your object)

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