WPF

2016-04-10 21 views
-1
ile MVVM kullanarak bağlanma sorunları

C# 'da yeniyim. Bağlamda sıkıntı yaşıyorum. Burada kod örneğini bırakacağım ve umarım sorunları bulmama yardım edersin.WPF

Arkadaşlarınızı

<UserControl x:Class="WpfWHERE.View.FriendsView" 
     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" 
     xmlns:local="clr-namespace:WpfWHERE.View" 
     xmlns:ViewModel="clr-namespace:WpfWHERE.ViewModel" 
     xmlns:data = "clr-namespace:WpfWHERE.Model" 
     mc:Ignorable="d" 
     d:DesignHeight="600" d:DesignWidth="800"> 
<UserControl.DataContext> 
    <ViewModel:FriendsViewModel/> 
</UserControl.DataContext> 
<UserControl.Resources><DataGrid x:Key="friendsList" AutoGenerateColumns="false" ItemsSource = "{Binding Student}"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn Header="Name" Width="150"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox x:Name="cbName" SelectedItem="{Binding Path=FullName, Mode=OneWay}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" DisplayMemberPath="Name"> 
         </ComboBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

FriendsViewModel

public class FriendsViewModel:AViewModel 
{ 
    #region fields 
    public DelegateCommand DeleteCommand { get; set; } 
    #endregion fields 

    #region constructors 
    public FriendsViewModel() 
    { 
     LoadStudents(); 
     DeleteCommand = new DelegateCommand(OnDelete, CanDelete); 
    } 
    #endregion constructors 
    public ObservableCollection<Student> Students 
    { 
     get; 
     set; 
    } 

    public void LoadStudents() 
    { 
     ObservableCollection<Student> students = new ObservableCollection<Student>(); 

     students.Add(new Student { FirstName = "Mark", LastName = "Allain" ,Place = "Home"}); 
     students.Add(new Student { FirstName = "Allen", LastName = "Brown", Place = "China" }); 
     students.Add(new Student { FirstName = "Linda", LastName = "Hamerski", Place = "Je" }); 

     Students = students; 
    } 

    private Student _selectedStudent; 

    public Student SelectedStudent 
    { 
     get 
     { 
      return _selectedStudent; 
     } 

     set 
     { 
      _selectedStudent = value; 
      DeleteCommand.RaiseCanExecuteChanged(); 
     } 
    } 

    private void OnDelete() 
    { 
     Students.Remove(SelectedStudent); 
    } 

    private bool CanDelete() 
    { 
     return SelectedStudent != null; 
    } 
} 

Öğrenci

public class StudentModel { } 

public class Student : INotifyPropertyChanged 
{ 
    private string firstName; 
    private string lastName; 
    private string place; 

    public string FirstName 
    { 
     get { return firstName; } 

     set 
     { 
      if (firstName != value) 
      { 
       firstName = value; 
       RaisePropertyChanged("FirstName"); 
       RaisePropertyChanged("FullName"); 
      } 
     } 
    } 

    public string LastName 
    { 
     get { return lastName; } 

     set 
     { 
      if (lastName != value) 
      { 
       lastName = value; 
       RaisePropertyChanged("LastName"); 
       RaisePropertyChanged("FullName"); 
      } 
     } 
    } 
    public string Place 
    { 
     get { return place; } 

     set 
     { 
      if (place != value) 
      { 
       place = value; 
       RaisePropertyChanged("Place"); 
      } 
     } 
    } 

    public string FullName 
    { 
     get 
     { 
      return firstName + " " + lastName; 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void RaisePropertyChanged(string property) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 

Gerçekten neyi yanlış yaptığımı bilmiyorum, google ve biraz biriktirdim, ancak çalışamam. Umarım bilgiyi paylaşabilir ve bana yardım edebilirsin. a) yerine ItemsSource = "{Binding Student}

b ItemsSource = "{Binding Students} kullanma

sözdizimi ile bir TextBlock kullanma) yorumlarda 2 değişikliklere dikkati alarak

Saygılarımızla,

+2

bile sorunun ne olduğunu söylemiyorlar gerçeğini atlanıyor etkilememeli, sen 'friendList' bağlayıcı ediyoruz "Öğrenci" adlı bir mülke ... ki ... ... mevcut değil. –

+0

Öğrenci benim modelim. Bunu bağlamaya çalışıyorum. Hala bu MVVM mimarisini öğreniyor ... – Antoine

+0

ItemsSource koleksiyonunuza ayarlanmalıdır. Bunu 'ItemsSource = "{Binding Students}" '(yani koleksiyonunuzun özellik adı) olarak değiştirin – Tone

cevap

2

<TextBlock Text = "{Binding Path = FullName, Mode = OneWay}" ></TextBlock>

tadil aşağıdaki FriendsView kodunuz için istenen DataGrid sonucunu vermeniz gerekir. FullName'u gösteren tek bir sütuna sahip bir DataGrid'i göstermek için test ettim ve çalışıyorum.

<UserControl x:Class="WpfWHERE.View.FriendsView" 
     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" 
     xmlns:local="clr-namespace:WpfWHERE.View" 
     xmlns:ViewModel="clr-namespace:WpfWHERE.ViewModel" 
     xmlns:data = "clr-namespace:WpfWHERE.Model" 
     mc:Ignorable="d" 
     d:DesignHeight="600" d:DesignWidth="800"> 
    <UserControl.DataContext> 
     <ViewModel:FriendsViewModel/> 
    </UserControl.DataContext> 
    <UserControl.Resources> 
     <DataGrid x:Key="friendsList" AutoGenerateColumns="false" ItemsSource = "{Binding Students}"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="Name" Width="150"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock Text = "{Binding Path = FullName, Mode = OneWay}" ></TextBlock> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
    </UserControl.Resources> 
    <ContentControl Content="{StaticResource friendsList}"/> 
</UserControl> 

Sadece not: Bunu görüntülüyorsak emin değilim ben bir ContentControl içinde DataGrid görüntülenen ettik

1).

2) ben size AViewModel ne var emin değilim ama sonuç

+0

Mükemmel, işe yarıyor. AViewModel, ortak ViewModelBase'dir. Bir İçerik denetimi kullanmak veya DataGrid'i doğrudan UserControl.Resources dışında koymak arasındaki fark nedir? – Antoine

+0

Genellikle, birden çok yerde yeniden kullanmak istiyorsanız 'UserControl.Resources' içinde' x: Key' ile bir şeyler tanımlarsınız. Sonra 'x: Key' yi kullanarak onu 'StaticResource' olarak adlandırırsınız. Eğer sadece bir kez kullanıyorsanız, DataGrid 'i UserControl.Resources' in dışına koyabilir ve 'x: Key '(' x: Key ')' in Kaynaklar bölümünün dışında geçerli değildir. Ben sadece 'ContentControl' içine koydum, bu yüzden kodunuzu eşleştirmek için x: anahtarını referans gösterebildim. – Tone

+0

harika bir açıklama. Sadece bir şey daha, şey App.xml'deki bir Style şablonu gibi başka görünümlerde veya sadece bir tane içinde yeniden kullanılabilir mi? – Antoine