简体   繁体   中英

WPF Core 3.1 bound TextBox not visible at runtime

Learning WPF Core 3.1 WPF MVVM pattern, using Visual Studio 2019 4.8.04084.

I have a MainWindow with the following (boilerplate excluded)

<Window>
    
    <Grid
        Width="1024"
        Height="768"
        Margin="0,0,0,0">

        <Grid.RowDefinitions>
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>

        <usercontrols:CustomerMaintenanceControl
            x:Name="CustomerMaintenance"
            Grid.Row="0"
            Height="109"
            VerticalAlignment="Top" />

    </Grid>
    
</Window> 

The CustomerMaintenanceControl user control looks like this:

<UserControl
    x:Class="Redacted.UserControls.CustomerMaintenanceControl"
    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:usercontrols="clr-namespace:Redacted.UserControls"
    xmlns:vm="clr-namespace:Redacted.ViewModels"
    d:DesignHeight="450"
    d:DesignWidth="800"
    Loaded="UserControl_Loaded"
    mc:Ignorable="d">
    <UserControl.Resources>
        <vm:CustomerMaintenanceViewModel x:Key="viewModel" />
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <usercontrols:CustomerMaintenanceGridControl
            x:Name="gridControl"
            Grid.Row="0"
            Height="200"
            DataContext="{StaticResource viewModel}" />

        <usercontrols:CustomerMaintenanceDetailControl
            x:Name="detailControl"
            Grid.Row="1"
            Width="800"
            Height="200"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            DataContext="{StaticResource viewModel}" />

    </Grid>
</UserControl>

Here's the CustomerMaintenanceDetailControl, where the issue is:

<UserControl
    x:Class="Redacted.UserControls.CustomerMaintenanceDetailControl"
    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:local="clr-namespace:Redacted"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <Grid Margin="0,0,0,325">
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>
        <TabControl
            x:Name="tabControl"
            Grid.Row="0"
            Margin="0,0,0,0">
            <TabItem>
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock>General</TextBlock>
                    </StackPanel>
                </TabItem.Header>
                <StackPanel>
                    <TextBox
                        x:Name="companynmTextbox"
                        Width="322"
                        Margin="0,32,228,0"
                        HorizontalAlignment="Left"
                        IsReadOnly="True"
                        Text="{Binding Path=Entity.Companynm}" />
                </StackPanel>
            </TabItem>
        </TabControl>
    </Grid>
</UserControl>

So the CustomerMaintenanceControl has two user controls in it, a CustomerMaintenanceGridControl that has a DataGrid and then a CustomerMaintenanceDetailControl which has a tab control and a bound TextBox. The grid is pulling data in fine, so the model and data layer are working. However the 'companyNmTextBox' control on CustomerMaintenanceGridControl is displaying nothing.

There are no binding errors in VS. In addition, when I run the application and open the Live Visual Tree, I can open the properties for 'companyNmTextBox' and under Inherited\DataContext I have my 'Customers' ObservableCollection there and populated, and my 'Entity' property on the model populated with the data for the first Customer. Yet nothing showing onscreen.

Since people are voting to close because of 'debugging details' (?) - the debugging details are in the last paragraph - there are no binding or errors in Visual Studio, and everything appears bound OK in the Live Visual Tree/ The 'desired behaviour' is that the textbox displays the value of the thing that it is bound to. If there are further debugging steps I could investigate I'd be delighted to hear them.

Your Entity has to implement INotifyPropertyChanged interface

public class Entity : INotifyPropertyChanged  
{  
    private string companynm;
    public string Companynm
    {  
        get  
        {  
            return companynm;  
        }  

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

Also TextBox companynmTextbox has the property IsReadOnly set to true, so in that case I would modify the binding.

Text="{Binding Path=Entity.Companynm, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"

It was a typo to the call to RaisePropertyChanged() in the ViewModel the CustomerMaintenanceDetailControl is using.

I had:

  private Customer _entity = new Customer();

    public Customer Entity
    {
        get { return _entity; }
        set
        {
            _entity = value;
            RaisePropertyChanged("Customer");  // <- problem here
        }
    }

I should have had:

   private Customer _entity = new Customer();

    public Customer Entity
    {
        get { return _entity; }
        set
        {
            _entity = value;
            RaisePropertyChanged("Entity");  // <- doh
        }
    }

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