WPF mvvm文本框不会更新

时间:2014-12-10 14:45:37

标签: c# wpf xaml mvvm textbox

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

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

但是当我在代码中更改varriable时,它不会在视图中更新它。 我在一个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"));
        }
    }
}

0 个答案:

没有答案
相关问题