2016-04-10 86 views
-1

我是新來的C#。 我在綁定中遇到麻煩。我將在這裏留下代碼示例,並希望您能幫助我找到麻煩。使用MVVM與WPF綁定問題

FriendsView

<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; 
    } 
} 

學生

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)); 
     } 
    } 
} 

我真的不知道我在做什麼錯,我谷歌和堆疊了一下,但不能使它的工作。我希望你能分享知識並幫助我。

最好的問候,

+2

跳過事實,你甚至沒有告訴是什麼問題,你要綁定'friendList'到名爲'Student'的屬性,該屬性不存在。 –

+0

學生是我的模特。我試圖綁定這個。仍在學習這種MVVM架構... – Antoine

+0

ItemsSource應該設置爲您的集合。將其更改爲'ItemsSource =「{綁定學生}」(即您收藏的屬性名稱) – Tone

回答

2

在評論中服用的這兩個變化音符

a)使用ItemsSource = "{Binding Students}代替ItemsSource = "{Binding Student}

b)使用一個TextBlock語法<TextBlock Text = "{Binding Path = FullName, Mode = OneWay}" ></TextBlock>

以下修改您的FriendsView的代碼應給出所需的DataGrid結果。經過測試並在我的最後階段顯示一個DataGrid,其中一列顯示了FullName

<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> 

只是要注意:

1)我已經顯示DataGridContentControl因爲我不知道你是如何顯示它。

2)我不知道你在你的AViewModel但它不應該影響結果

+0

完美,它的作品。 AViewModel是常見的ViewModelBase。使用Content控件或將DataGrid直接放在UserControl.Resources之外有什麼區別? – Antoine

+0

如果你想在多個地方重複使用,你通常在'UserControl.Resources'中用'x:Key'定義一些東西。然後你使用'x:Key'把它稱爲'StaticResource'。如果只使用它,只需將DataGrid放在UserControl.Resources之外,並省略「x:Key」(x:Key無法在「資源」部分之外有效)即可。我只是把它放在一個'ContentControl'中,所以我可以引用'x:key'來匹配你的代碼。 – Tone

+0

真棒解釋。還有一件事,它可以在其他視圖中重用,如App.xml中的Style模板或者其他視圖? – Antoine