簡體   English   中英

WPF Core 3.1 綁定的文本框在運行時不可見

[英]WPF Core 3.1 bound TextBox not visible at runtime

使用 Visual Studio 2019 4.8.04084 學習 WPF Core 3.1 WPF MVVM 模式。

我有一個帶有以下內容的 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> 

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>

這是 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>

因此,CustomerMaintenanceControl 中有兩個用戶控件,一個是具有 DataGrid 的 CustomerMaintenanceGridControl,另一個是具有選項卡控件和綁定的 TextBox 的 CustomerMaintenanceDetailControl。 網格很好地提取數據,因此 model 和數據層正在工作。 但是,CustomerMaintenanceGridControl 上的“companyNmTextBox”控件不顯示任何內容。

VS中沒有綁定錯誤。 此外,當我運行應用程序並打開 Live Visual Tree 時,我可以打開“companyNmTextBox”的屬性,在 Inherited\DataContext 下我有我的“客戶”ObservableCollection 並填充,我在 model 上的“實體”屬性填充與第一個客戶的數據。 然而,屏幕上沒有任何顯示。

由於人們因為“調試細節”(?)而投票關閉 - 調試細節在最后一段 - Visual Studio 中沒有綁定或錯誤,並且在 Live Visual Tree/“期望的行為”中一切看起來都綁定了' 是文本框顯示它所綁定的事物的值。 如果有進一步的調試步驟我可以調查我很高興聽到它們。

您的實體必須實現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));
    }  
}

TextBox companynmTextbox的屬性IsReadOnly設置為 true,因此在這種情況下,我將修改綁定。

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

這是對 CustomerMaintenanceDetailControl 正在使用的 ViewModel 中的 RaisePropertyChanged() 調用的拼寫錯誤。

我有:

  private Customer _entity = new Customer();

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

我應該有:

   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