获取在一个方法中显示的值调用XAML WPF

时间:2014-10-21 12:23:51

标签: c# .net wpf xaml mvvm

正如标题所说,我试图通过数据绑定显示我在XAML文件中输入的硬编码值,但在1方法调用中,而不是在每个特定属性中绑定文本框。

型号:

    public class Person
{
    public string Namn { get; set; }
    public DateTime Födelsedatum { get; set; }
    public int Betyg { get; set; }
    public int AntalBarn { get; set; }
    public int Favoritsiffra { get; set; }
    public string Kommentar { get; set; }
}

查看:

<Window x:Class="PiedPiper.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Inlämningsuppgift WPF" Height="400" Width="400">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Label Content="Personuppgifter" FontSize="35" Grid.Column="0" Grid.Row="0"></Label>
    <Separator Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Margin="10,2,-50,2"/>
    <Label Content="Namn:" Grid.Column="0" Grid.Row="2" Margin="5,5,210,0"></Label>
    <TextBox Name="NamnTextBox" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="118,5,-50,0"/>
    <Label Content="Födelsedatum:" Grid.Column="0" Grid.Row="3" Margin="5,5,210,0"></Label>
    <TextBox Name="FödelsedatumTextBox" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Margin="118,5,-50,0"></TextBox>
    <Label Content="Betyg" Grid.Column="0" Grid.Row="4" Margin="5,5,210,0"></Label>
    <ComboBox Name="BetygComboBox" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="218,5,-50,0"></ComboBox>
    <Label Content="Antal barn (0-20):" Grid.Column="0" Grid.Row="5" Margin="5,5,210,0"></Label>
    <Slider Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Margin="125,5,25,0"></Slider>
    <TextBox Name="AntalBarnTextBox"  Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Margin="300,5,-50,0"></TextBox>
    <Label Content="Favoritsiffra (0-99):" Grid.Column="0" Grid.Row="6" Margin="5,5,210,0"></Label>
    <TextBox Name="FavoritsiffraTextBox"  Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2" Margin="225,5,-50,0"></TextBox>
    <CheckBox Content="Visa kommentar" Grid.Row="7" Grid.Column="0"  Margin="5,5,210,0"></CheckBox>
    <TextBox Name="KommentarTextBox"  Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" Margin="125,5,-50,-50" Grid.RowSpan="2"></TextBox>
    <Button Content="Spara" Grid.Row="8" Grid.Column="0" Margin="125,55,-50,-80" Grid.ColumnSpan="2"></Button>
    <Separator Grid.Row="9" Grid.Column="0" Grid.ColumnSpan="2" Margin="10,85,-50,-80"></Separator>
    <Label Content="Andre Kordasti" Grid.Row="10" Grid.Column="0" Margin="10,85,-50,-80"></Label>
    <Button Content="Avsluta" Grid.Row="10" Grid.Column="0" Margin="225,85,-50,-80" Grid.ColumnSpan="2"></Button>
</Grid>

视图模型:

     public class PersonViewModel : INotifyPropertyChanged
    {
        public PersonViewModel(Person person)
        {
            Namn = person.Namn;
            Födelsedatum = person.Födelsedatum;
            Betyg = person.Betyg;
            AntalBarn = person.AntalBarn;
            Favoritsiffra = person.Favoritsiffra;
            Kommentar = person.Kommentar;

            GetPerson();
        }

        private void GetPerson()
        {
            Namn = "KurtSune";
            Födelsedatum = new DateTime(1980, 09, 06);
            Betyg = 3;
            AntalBarn = 7;
            Favoritsiffra = 10;
            Kommentar = "Kommentar...";

            var mainWindow = new MainWindow();

            mainWindow.NamnTextBox.Text = Namn;
            mainWindow.FödelsedatumTextBox.Text = Convert.ToString(Födelsedatum);
            mainWindow.BetygComboBox.SelectedValue = Betyg;
            mainWindow.AntalBarnTextBox.Text = Convert.ToString(AntalBarn);
            mainWindow.FavoritsiffraTextBox.Text = Convert.ToString(Favoritsiffra);
            mainWindow.KommentarTextBox.Text = Kommentar;
        }

        public ICommand SaveCommand { get; set; }
        public ICommand AbortCommand { get; set; }

        public bool CanSave
        {
            get
            {
                if (Namn == null)
                {
                    return false;
                }

                return !String.IsNullOrWhiteSpace(Namn);
            }
        }

        private string _namn;
        public string Namn
        {
            get { return _namn; }
            set { _namn = value; OnPropertyChanged("Namn"); }
        }

        private DateTime _födelsedatum;
        public DateTime Födelsedatum
        {
            get { return _födelsedatum; }
            set { _födelsedatum = value; OnPropertyChanged("Födelsedatum"); }
        }

        private int _betyg;
        public int Betyg
        {
            get { return _betyg; }
            set { _betyg = value; OnPropertyChanged("Betyg"); }
        }


        private int _antalBarn;
        public int AntalBarn
        {
            get { return _antalBarn; }
            set { _antalBarn = value; OnPropertyChanged("AntalBarn"); }
        }


        private int _favoritSiffra;
        public int Favoritsiffra
        {
            get { return _favoritSiffra; }
            set { _favoritSiffra = value; OnPropertyChanged("Födelsedatum"); }
        }

        private string _kommentar;
        public string Kommentar
        {
            get { return _kommentar; }
            set { _kommentar = value; OnPropertyChanged("Kommentar"); }
        }

        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

并添加我的App.xaml.cs文件:

    public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        var person = new Person();

        var personViewModel = new PersonViewModel(person);

        var mainWindow = new MainWindow();

        mainWindow.DataContext = personViewModel;

        mainWindow.Show();
    }
}

所以我没有得到以下值来展示......请帮忙,我是新手。

1 个答案:

答案 0 :(得分:0)

这是一个简化的例子,其中,

我创建了一个DataTemplate,其中控件绑定到Person属性。好处是显而易见的,因为您将看到修改的任何一个结束,另一个将知道/显示这些变化。

(添加了一个按钮和一个MessageBox以进一步证明这一点)

代码:

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            var person = new Person {Name = "name1", Address = "address1"};
            person.PropertyChanged += person_PropertyChanged;
            DataContext = person;
        }

        private void person_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            MessageBox.Show("data changed");
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var dataContext = (Person) DataContext;
            dataContext.Name = "newName";
            dataContext.Address = "newAddress";
        }
    }

    internal class Person : INotifyPropertyChanged
    {
        private string _address;
        private string _name;

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

        public string Address
        {
            get { return _address; }
            set
            {
                _address = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication1="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Width="525"
        Height="350">
    <Window.Resources>
        <DataTemplate x:Key="PersonTemplate" DataType="wpfApplication1:Person">
            <StackPanel>
                <TextBlock Text="Name" />
                <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
                <TextBlock Text="Address" />
                <TextBox Text="{Binding Address, UpdateSourceTrigger=PropertyChanged}" />
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Click="ButtonBase_OnClick" Content="Modify" />
            <ContentControl Content="{Binding}" ContentTemplate="{StaticResource PersonTemplate}" />
        </StackPanel>
    </Grid>
</Window>

现在您可以看到XAML中没有硬编码值,我传递给DataContext的对象改为使用样本值。