Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF mvvm文本框不会更新_C#_Wpf_Xaml_Mvvm_Textbox - Fatal编程技术网

C# WPF mvvm文本框不会更新

C# WPF mvvm文本框不会更新,c#,wpf,xaml,mvvm,textbox,C#,Wpf,Xaml,Mvvm,Textbox,我正在尝试清空/填充WPF mmvm中的文本框。我创建了一个小表单,如果按下按钮,文本框中的项目将保存在对象类别中。创建类别后,每个已创建类别的名称将放在一个组合框中。现在,我要做的是在创建类别后清空文本框,或者如果我在de combobox中单击类别名称,则在文本框中显示信息 我得到了保存类别工作的部分,以及如果我从组合框中选择一个项目,我会收到通知的部分 但是当我在代码中更改变量时,它不会在视图中更新它。 我在resource.xaml文件中连接了Viewmodel和view: <Da

我正在尝试清空/填充WPF mmvm中的文本框。我创建了一个小表单,如果按下按钮,文本框中的项目将保存在对象类别中。创建类别后,每个已创建类别的名称将放在一个组合框中。现在,我要做的是在创建类别后清空文本框,或者如果我在de combobox中单击类别名称,则在文本框中显示信息

我得到了保存类别工作的部分,以及如果我从组合框中选择一个项目,我会收到通知的部分

但是当我在代码中更改变量时,它不会在视图中更新它。 我在resource.xaml文件中连接了Viewmodel和view:

<DataTemplate DataType="{x:Type vm:CategoryViewModel}">
    <vw:CategoryView />
</DataTemplate>
我的模型视图:

class CategoryViewModel : INotifyPropertyChanged
{
    private string Title, Description, Colour;


    public string title
    {
        get { return Title; }
        set
        {
            Title = value; NotifyPropertyChanged("title");
        }
    }


    public string description { get { return Description; } set { Description = value; NotifyPropertyChanged("description"); } }
    public string colour { get { return Colour; } set { Colour = value; NotifyPropertyChanged("colour"); } }
    //public event PropertyChangedEventHandler PropertyChanged;
    private ObservableCollection<CategoryModel> category = new ObservableCollection<CategoryModel>();

    private CategoryModel selectedItem = new CategoryModel();

    public ObservableCollection<CategoryModel> Category
    {
        get {
            return category; 
        }
        set { 
            category = value; 
            NotifyPropertyChanged("Category"); 
        }
    }
    public CategoryModel SelectedItem
    {
        get { 
            return selectedItem; 
        }
        set { 
            selectedItem = value;
            NotifyPropertyChanged("SelectedItem");
            ChangeCategoryInfo();
        }
    }

    public CategoryViewModel()
    {
    }

    public ICommand btnSaveCategory
    {
        get { return new DelegateCommand<string>(BtnSaveCategory); }
    }

    public void BtnSaveCategory(object param)
    {
        Category.Add(new CategoryModel(title, description, colour));
    }

    private void ChangeCategoryInfo()
    {

    }

    public event PropertyChangedEventHandler PropertyChangedd;
    protected void NotifyPropertyChanged(params string[] propertyNames)
    {
        if (PropertyChangedd != null)
        {
            foreach (string propertyName in propertyNames) PropertyChangedd(this, new PropertyChangedEventArgs(propertyName));
            PropertyChangedd(this, new PropertyChangedEventArgs("HasError"));
        }
    }
}
我的看法是:

<UserControl x:Class="IsalaIndoorNavCreator.Views.CategoryView"
         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" 
         mc:Ignorable="d" Width="219.831" Height="440">
<Grid Margin="0,0,10,0">
    <Label Content="Naam" HorizontalAlignment="Left" Margin="10,33,0,0" VerticalAlignment="Top"/>
    <ComboBox x:Name="comboCategorys" 
        HorizontalAlignment="Left" 
        Margin="84,9,0,0" 
        VerticalAlignment="Top" 
        Width="120" 
        ItemsSource="{Binding Category}" 
        SelectedItem="{Binding SelectedItem}"
        IsSynchronizedWithCurrentItem="True"            
        >
    </ComboBox>
    <TextBox x:Name="txtbCategoryName" 
             HorizontalAlignment="Left" 
             Height="23" 
             Margin="88,36,0,0" 
             TextWrapping="Wrap" 
             VerticalAlignment="Top" 
             Width="115" 
             Text="{Binding Path=title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             />
    <Label Content="Beschrijving" HorizontalAlignment="Left" Margin="10,64,0,0" VerticalAlignment="Top"/>
    <Label Content="Kleur" HorizontalAlignment="Left" Margin="15,196,0,0" VerticalAlignment="Top"/>
    <TextBox 
        HorizontalAlignment="Left" 
        Height="23"
        Margin="94,200,0,0" 
        TextWrapping="Wrap" 
        VerticalAlignment="Top" 
        Width="109"
        Text="{Binding Path=colour, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
        />
    <Button Content="" HorizontalAlignment="Left" Margin="57,200,0,0" VerticalAlignment="Top" Width="27">
        <Button.Background>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFF3F3F3" Offset="0"/>
                <GradientStop Color="#FFEBEBEB"/>
                <GradientStop Color="#FFDDDDDD"/>
                <GradientStop Color="#FFFF0808" Offset="1"/>
            </LinearGradientBrush>
        </Button.Background>
    </Button>
    <Label Content="Afbeelding" HorizontalAlignment="Left" Margin="10,235,0,0" VerticalAlignment="Top"/>
    <Button Content="Selecteer afbeelding" HorizontalAlignment="Left" Margin="84,235,0,0" VerticalAlignment="Top" Width="119"/>
    <Image HorizontalAlignment="Left" Height="100" Margin="103,262,0,0" VerticalAlignment="Top" Width="100" Source="../Resources/Placeholder.png"/>
    <Button x:Name="btnSaveCategory" 
            Content="Opslaan" 
            HorizontalAlignment="Left" 
            Margin="128,381,0,0" 
            VerticalAlignment="Top" 
            Width="75" 
            Height="49" 
            Command="{Binding btnSaveCategory}"
            />
    <TextBox 
        x:Name="txtCategoryDescription" 
        HorizontalAlignment="Left"
        Height="116" 
        Margin="88,64,0,0" 
        TextWrapping="Wrap" 
        VerticalAlignment="Top" 
        Width="115"
        Text="{Binding Path=description, UpdateSourceTrigger=PropertyChanged}"
        />

</Grid>
我的模型:

class CategoryModel : BaseModel
{
    private string name, description, colour;

    public string Colour 
    { 
        get{ 
            return colour; 
        }
        set
        {
            if (colour != value)
            {
                colour = value;
                NotifyPropertyChanged("Colour");
            }
        }
    }
    public string Name 
    {
        get{ 
            return name; 
        }
        set
        {
            if (name != value)
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        } 
    }
    public string Description 
    { 
        get { 
            return description; 
        }
        set
        {
            if (description != value)
            {
                description = value;
                NotifyPropertyChanged("Description");
            }
        } 
    }
    public List<InterestingModel> iMList = new List<InterestingModel>();

    public CategoryModel()
    {

    }

    public CategoryModel(string name, string description, string colour)
    {
        this.name = name;
        this.description = description;
        this.colour = colour;
    }

    public void AddInterestingModel(InterestingModel IM)
    {
        iMList.Add(IM);
    }

    public override string ToString()
    {
        return name;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(params string[] propertyNames)
    {
        if (PropertyChanged != null)
        {
            foreach (string propertyName in propertyNames) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            PropertyChanged(this, new PropertyChangedEventArgs("HasError"));
        }
    }
}

看起来您实现了INotifyPropertyChanged的成员,但实际上没有将接口添加到类签名中。它是由BaseModel实现的吗?如果没有,则需要将其添加到签名中。如果是这样,子类中的事件可能隐藏了实际绑定到接口成员的基类中的事件,在这种情况下,WPF可能正在侦听与您发布的事件不同的事件;我看到你在帖子里更新了班级签名。您仍然有问题吗?没有,现在正在工作: