Autor Beitrag
Flier
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Fr 12.07.13 10:26 
Hallo.

Ich habe folgendes Problem und ich finde zurzeit auch keine Lösung dafür:

Ich möchte gerne in meiner View auf den Button Klicken und im Model soll der Wert dann geändert und an die TextBox zurück gegeben werden.

Hier der Quellcode:

View:
ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
<Window x:Class="MVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
   
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="188,34,0,0" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Top" Width="120"/>
        
        <Button Content="1" HorizontalAlignment="Left" Margin="215,185,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
    
</Window>


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ErgebnisViewModel(Model.CreatePerson());
        }
    }
}

View Model:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
    class ErgebnisViewModel : INotifyPropertyChanged
    {
        private Model _Model;

        //  Konstruktor

        public ErgebnisViewModel(Model model)
        {
            _Model = model;
            _Ergebnis = _Model.Ergebnis;          
        }

        // Schnittstellen-Ereignis

        public event PropertyChangedEventHandler PropertyChanged;

        protected internal void OnPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
                PropertyChanged(thisnew PropertyChangedEventArgs(propertyname));
        }

        // Eigenschaften

        private string _Ergebnis;
        public string Name
        {
            get { return _Ergebnis; }
            set
            {
                if (_Ergebnis == value) return;
                _Ergebnis = value;
                OnPropertyChanged("Ergebnis");
            }
        }
    }


und Model:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
    public class Model
    {        
            public string Ergebnis { get; set; }
            
            public static Model CreatePerson()
            {
                return new Model { Ergebnis = "0"};
            }
    }

Danke für jede Antwort.

Gruß Flier :)

Moderiert von user profile iconTh69: Beitragsformatierung überarbeitet.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 12.07.13 12:12 
Hallo Flier :welcome:

generell hast du ja MVVM (soweit ich das überblicke) ja bisher richtig implementiert.
Du mußt für den Button-Click entweder ein Click-Ereignis hinzufügen und im CodeBehind dann über das ViewModel die Werte ändern (so daß dann durch das DataBinding der geänderte Wert automatisch in der TextBox erscheint), s. z.B. WPF Tutorial: Ereignisbehandlung. Oder aber MVVM entsprechend ein Command erzeugen und daran binden, s. z.B. How to use Commands in WPF.

P.S: Bitte Crossposts immer angeben: myCSharp.de - MVVM - Mit Button Wert in meiner View verändern