简体   繁体   English

WPF Core 3.1 绑定的文本框在运行时不可见

[英]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.使用 Visual Studio 2019 4.8.04084 学习 WPF Core 3.1 WPF MVVM 模式。

I have a MainWindow with the following (boilerplate excluded)我有一个带有以下内容的 MainWindow(不包括样板文件)

<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: CustomerMaintenanceControl 用户控件如下所示:

<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:这是 CustomerMaintenanceDetailControl,问题出在:

<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.因此,CustomerMaintenanceControl 中有两个用户控件,一个是具有 DataGrid 的 CustomerMaintenanceGridControl,另一个是具有选项卡控件和绑定的 TextBox 的 CustomerMaintenanceDetailControl。 The grid is pulling data in fine, so the model and data layer are working.网格很好地提取数据,因此 model 和数据层正在工作。 However the 'companyNmTextBox' control on CustomerMaintenanceGridControl is displaying nothing.但是,CustomerMaintenanceGridControl 上的“companyNmTextBox”控件不显示任何内容。

There are no binding errors in VS. 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.此外,当我运行应用程序并打开 Live Visual Tree 时,我可以打开“companyNmTextBox”的属性,在 Inherited\DataContext 下我有我的“客户”ObservableCollection 并填充,我在 model 上的“实体”属性填充与第一个客户的数据。 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.由于人们因为“调试细节”(?)而投票关闭 - 调试细节在最后一段 - Visual Studio 中没有绑定或错误,并且在 Live Visual Tree/“期望的行为”中一切看起来都绑定了' 是文本框显示它所绑定的事物的值。 If there are further debugging steps I could investigate I'd be delighted to hear them.如果有进一步的调试步骤我可以调查我很高兴听到它们。

Your Entity has to implement INotifyPropertyChanged interface您的实体必须实现INotifyPropertyChanged接口

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. TextBox companynmTextbox的属性IsReadOnly设置为 true,因此在这种情况下,我将修改绑定。

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.这是对 CustomerMaintenanceDetailControl 正在使用的 ViewModel 中的 RaisePropertyChanged() 调用的拼写错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM