locked
DataTemplate RRS feed

  • שאלה

  • יש לי אובייקט בשם Person שמכיל שדה של שם ושדה של סיסמא. 

    אני רוצה להציג את האובייקט בתוך CmboBox למשל , ע"י  DataTemplate. 

    את זה אני מצליח לעשות והכל עובד כשורה , 

    אני מצליח להכניס את השם 

    אני שואל: איך אני יכול להכניס את השם והסיסמא לדוגמא כך: Name+"  "+"Password.

    <Window x:Class="INotifyPropertyChanged_learn.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 x:Name="Gridon">
            <Label Content="User"   Height="31" HorizontalAlignment="Left" Margin="30,26,0,0" Name="label1" VerticalAlignment="Top" Width="110" />
            <Label Content="Password" Height="29" HorizontalAlignment="Left" Margin="27,88,0,0" Name="label2" VerticalAlignment="Top" Width="116" />
            <TextBox Height="31" Text="{Binding Name}" HorizontalAlignment="Left" Margin="144,30,0,0" Name="textBox1" VerticalAlignment="Top" Width="132" />
            <TextBox Height="33" Text="{Binding Password}" HorizontalAlignment="Left" Margin="142,82,0,0" Name="textBox2" VerticalAlignment="Top" Width="135" />
            <Button Content="close"  Height="40" HorizontalAlignment="Left" Margin="66,232,0,0" Name="button1" VerticalAlignment="Top" Width="124" Click="but1_click_close" />
            <Button Content="change pass" Height="42" HorizontalAlignment="Left" Margin="251,230,0,0" Name="button2" VerticalAlignment="Top" Width="122" Click="change_pass_click" />
            <ComboBox ItemsSource="{Binding}" Height="30" HorizontalAlignment="Left" Margin="76,136,0,0" Name="comboBox1" VerticalAlignment="Top" Width="311" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderThickness="3" BorderBrush="{Binding Password}">
                        <TextBox Text="{Binding Name}"/>
                        </Border>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <Button Content="Add" Height="34" HorizontalAlignment="Left" Margin="320,57,0,0" Name="button3" VerticalAlignment="Top" Width="57" Click="Add_butt_CLick" />
        </Grid>
    </Window>
    ==============
    
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    
    namespace INotifyPropertyChanged_learn
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            Person person;
            ObservableCollection<Person> personList = new ObservableCollection<Person>(); 
            public MainWindow()
            {
    
                InitializeComponent();
                  
                person = new Person { Name = "amiel", Password = "cohen" };
                Gridon.DataContext = personList;     
            
            }
    
            private void but1_click_close(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
    
            private void change_pass_click(object sender, RoutedEventArgs e)
            {
                person.Password = "3456";
                MessageBox.Show(person.Password); 
            }
    
            private void Add_butt_CLick(object sender, RoutedEventArgs e)
            {
                personList.Add(
                new Person { Name = textBox1.Text, Password = textBox2.Text }
                    );
                MessageBox.Show(personList.Count.ToString());
            }
    
    
                    
        }
    
        public class Person:INotifyPropertyChanged
        {
            private string name;
            private string password;
    
            public string Name
            {
                get { return name; }
                set {
                    name = value;
                    if (PropertyChanged!= null)
                    {
                        PropertyChanged(this,new PropertyChangedEventArgs("Name"));
                    }
                }
            }
    
            public string Password
            {
                get { return password; }
                set {
                    password = value;
                    if (PropertyChanged!=null)
                    {
                        PropertyChanged(this,new PropertyChangedEventArgs("Password"));
                    }
                }
            }
    
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
    }
    

    יום שלישי 15 מאי 2012 11:42

תשובות