2013-02-10 60 views
0

我有我的項目中以下類,EF5 + ListView和實體

public class Area 
{ 
    [Key] 
    public int Id { get; set; } 
    [Required] 
    public string Name { get; set; } 

    public ObservableCollection<VisitDetail> VisitDetails { get; set; } 
} 

public class VisitDetail 
{ 
    [Key] 
    public int VisitId { get; set; } 
    [Required] 
    public int AreaId { get; set; } 

    public Area Area { get; set; } 
} 

用戶要保存其參觀區使用下面的方法的日期。

enter image description here

我想只得到選定地區從ListView保存它們。當我試圖讓那些使用ListView.Items[index].IsSelected它拋出一個錯誤的說法,

Unable to cast object of type 'Namespace.Area' to type 'System.Windows.Controls.ListViewItem' 

請告訴我解決我的問題的具體辦法。

編輯1:

我的項目是在WPF。並且注意這一點,當訪問詳細信息窗口加載時,Area實體集合被綁定到ListView.ItemsSource。 (因爲WPF的沒有任何ListView.CheckedItems:(

編輯2:

感謝@blins您的解決方案工作。但我無法得到檢查的項目。我在這裏發佈我的xaml。不過,我可以得到選定的項目。如果我能得到那些檢查過的物品,那對我來說會很高興。

這是我的ListView

<ListView Name="lvList" SelectionMode="Multiple" ClipToBounds="True" > 
    <ListView.View> 
     <GridView > 
      <GridViewColumn DisplayMemberBinding="{Binding Id}" /> 
      <GridViewColumn Header="Area" > 
       <GridViewColumn.CellTemplate> 
        <DataTemplate > 
         <CheckBox x:Name="checkbox" Content="{Binding Name}" IsChecked="{Binding Path=IsSelected}" /> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
     </GridView> 
    </ListView.View> 
</ListView> 

XAML我想應該是我的問題的解決方案。

+0

我的WPF是一個小rusty-可以爲您提供請爲ListView提供XAML代碼。 – 2013-02-10 19:41:37

+0

如何綁定ListView中的項目?也許你應該發佈你的XAML來跟隨你的榜樣。儘管如此,我的答案解決了你當前的鑄造問題。 – blins 2013-02-10 20:15:31

回答

0

拋出異常是因爲ListView的Items屬性實際上表示用於生成列表內容的項目列表(即它們不是ListViewItem類型,而是您的區域類型)。這只是WPF的操作方式,在這種情況下您需要的是向潛在的ItemsControl的ItemContainerGenerator詢問與UI中的項目對應的ListViewItem。

XAML:

<ListView Name="listView1" /> 

代碼(假設你已經設置ListView控件的DataContext的和的ItemsSource等):

ListViewItem item = listView1 
    .ItemContainerGenerator 
    .ContainerFromIndex(0) as ListViewItem; 

item.IsSelected = true; 

而一個選擇,如果你不知道/護理列表中的索引而您知道該項目。

ListViewItem item = listView1 
    .ItemContainerGenerator 
    .ContainerFromItem(someAreaInstance) as ListViewItem; 

回答問題2部分:

你真的應該看看MVVM模式,這將有助於你避免這種情況,開始用,但這裏更深入的是你所追求的一種方式。首先,你可能會想一個輔助方法類似如下(未測試):

static FrameworkElement GetVisualDescendantByName(DependencyObject control, string name) 
{ 
    // Recurse 
    FrameworkElement el = null; 
    int nChildren = VisualTreeHelper.GetChildrenCount(control); 
    for (int i = 0; i < nChildren; i++) 
    { 
     var child = VisualTreeHelper.GetChild(control, i); 
     el = GetVisualDescendantByName(child, name); 
     if (el != null) 
      return el; 
    } 

    // See if control is a match 
    if (control is FrameworkElement) 
    { 
     el = control as FrameworkElement; 
     if (el.Name == name) 
      return control as FrameworkElemdent; 
    } 

    return null; 
} 

的,你可以做這樣的事情......

foreach (var item in lvList.Items) 
{ 
    var listItem = lvList.ItemContainerGenerator 
     .ContainerFromItem(item) as ListViewItem; 

    CheckBox cb = GetVisualDescendantByName(listItem, "checkbox") as CheckBox; 

    // Do stuff with CheckBox... 
    var myVar = cb.IsChecked; 
} 
+0

謝謝。您的解決方案奏效但請看我的第二個編輯。你對此有何看法? – 2013-02-11 05:30:30

+0

好的,你問題的第二部分的答案稍微複雜一些。我會稍後嘗試編輯我的答案並詳細闡述一下。 – blins 2013-02-11 13:50:38

+0

好的。謝謝@blins。我會嘗試你的解決方案。 – 2013-02-12 20:43:25