2014-08-27 70 views
1

我想顯示一個wpf-Listview中的對象集合,並提供一個組合框來選擇其他集合中的值。列表視圖中的mvvm組合框選擇

一些代碼來說明我的問題:

XAML: (Window gets its DataContext set in app.xaml.cs) 

<ListView ItemsSource="{Binding Path=Books}" SelectedItem="{Binding Path=CurrentBook}"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="author" DisplayMemberBinding="{Binding author}" /> 
      <GridViewColumn Header="title" DisplayMemberBinding="{Binding title}" /> 
      <GridViewColumn Header="genre"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Genres}" IsSynchronizedWithCurrentItem="true" DisplayMemberPath="genreName" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CurrentBook_Genre}"> 
         <ComboBox.ItemTemplate> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding genreName}" /> 
           <TextBlock Text="{Binding genreDescription}" /> 
          </StackPanel> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
     </GridView> 
    </ListView.View> 
</ListView> 

現在是「CurrentBook_Genre」的MainWindowViewModel的屬性,在ListView顯示項目的依據價值是由它的制定者設定。

就像在ListView的每個「行」中都顯示相同的ComboBox一樣。 我想我需要某種「EditValueTemplate」和「ShowValueTemplate」以及某種選擇器,如果ComboBox僅在處於編輯模式的行中顯示 - 我想這是一件常見的事情對於大多數DataDriven應用程序,所以也許有一種方法更容易iam不知道。

對此的唯一很好的解釋是 http://tech.pro/tutorial/857/wpf-tutorial-using-the-listview-part-3-in-place-edit 但他們正在使用依賴性屬性,反對我使用的ViewModel包裝模型。

+0

*他們正在使用依賴屬性,與ViewModel-wrapped模型相反,使用* ...一個'DependencyProperty'不是*反對*到一個正常的屬性,它們都以相同的方式工作,綁定路徑。如果您無法調整您找到的解決方案來解決您自己的問題,那麼您將成爲開發人員的麻煩。 – Sheridan 2014-08-27 11:09:21

+0

Iam現在閱讀關於他們,謝謝你的關注。每天都有新的概念和名字 - 如果我理解了基本的工具和概念,那麼它就會隨着適應而變得更好。 – rrrrn 2014-08-27 11:32:20

+0

這聽起來像是你的問題是因爲你的數據在你的'MainWindowViewModel'類的'ComboBox.SelectedItem'(或類似的)屬性綁定了'GridView'中每個'ComboBox'的屬性。解決方法是將該屬性的副本(或將其移動)添加到「Book」類中,以便每個「Book」項都擁有它自己的屬性,並知道哪個項目在其自己的「ComboBox」中被選中。 – Sheridan 2014-08-27 12:01:09

回答

0

當網格行被編輯時,有一種更簡單的方式可以讓一個控件顯示,而另一個則不會比您找到的例子更好。試試這個:

<DataGrid ItemsSource="{Binding Books}" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CurrentBook_Genre.genreName}" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
      <DataGridTemplateColumn.CellEditingTemplate> 
       <DataTemplate> 
        <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Genres}" IsSynchronizedWithCurrentItem="true" DisplayMemberPath="genreName" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CurrentBook_Genre}" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellEditingTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

這將顯示一個ComboBox當行正在編輯和一個普通的老TextBlock時,它不是。