繁体   English   中英

我无法将 class 属性绑定到文本框(MVVM、WPF、C#)

[英]I can't bind class properties into text boxes (MVVM, WPF, C#)

我正在学习 MVVM 模式,我有一个简单的问题。

我创建了一个 class,我想将其属性绑定到对话框内的文本框中(在这种情况下,我只有一个属性):

public class NewObjectClass : ObservableObject
{

    private string _name;

    public string Name
    {
        get { return _name; }
        set 
        { 
            _name = value;
            OnPropertyChanged("Name");
        }
    }
}

这是对话框的 ViewModel:

class ObjCreationViewModel : ObservableObject
{

    // Class whose properties I want to bind in text boxes
    private NewObjectClass _newObject;
    public NewObjectClass NewObject
    {
        get { return _newObject; }
        set { _newObject = value;
            OnPropertyChanged("NewObject");
        }
    }

    // String to test binding
    private string _test;
    public string Test
    {
        get { return _test; }
        set { _test = value; }
    }


    // Create new object view
    private DelegateCommand _createNewObject;
    public DelegateCommand CreateNewObjectCmd => _createNewObject ?? (_createNewObject = new DelegateCommand(CreateNewObject));
    void CreateNewObject()
    {

        // Do things in the future

    }

    public ObjCreationViewModel()
    {
        // Instance of New Object
        NewObjectClass NewObject = new NewObjectClass();

        // New object property that I want to show in TextBox
        NewObject.Name = "Why this one is not working?";

        // String text that works
        Test = "Why this is working?";

    }

}

最后是视图:

<UserControl x:Class="AOE.MVVM.View.ObjCreationView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:AOE.MVVM.View" 
         xmlns:viewmodel="clr-namespace:AOE.MVVM.ViewModel"
         mc:Ignorable="d"
         MinHeight="500" MinWidth="500">
<UserControl.DataContext>
    <viewmodel:ObjCreationViewModel/>
</UserControl.DataContext>
<Border Grid.ColumnSpan="4" CornerRadius="20"
            Background="White" BorderBrush="Black" BorderThickness="1.5">
    <Grid HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="1">
            <StackPanel Orientation="Horizontal">
                <Label Margin="10"  Content="Object name:" Style="{StaticResource DescriptionLabelStyle}"/>
                <TextBox x:Name="TextObjectName" Width="200" Height="25" Text="{Binding NewObject.Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
            </StackPanel>
        </StackPanel>
        <Button Grid.Row="1" Margin="5" Name="CreateBtn" Width="75" Height="30" Content="Create" FontFamily="Verdana" Foreground="#007BC0" FontWeight="Bold" Background="Transparent" BorderThickness="3" BorderBrush="#007BC0" VerticalAlignment="Bottom" Command="{Binding CreateNewObjectCmd}"/>
    </Grid>
</Border>

问题是我无法将“NewObject”的属性名称绑定到文本框中,但是如果我绑定“Test”,它可以正常工作。

我想在文本框中显示属性值,如果我更改文本框中的值,请更改实例“NewObject”的属性值。

任何人都可以告诉我有什么问题吗?

PD“实现 PropertyChange 事件的 ObservableObject class”:

public class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName] string propertyname = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
    }
}

线

NewObjectClass NewObject = new NewObjectClass();

定义了一个局部变量NewObject ,但没有设置属性NewObject

将其更改为

NewObject = new NewObjectClass();

暂无
暂无

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

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