2013-02-14 83 views
0

我正在使用Microsoft Access和WPF的ERP類型系統。我對如何將信息綁定到某些文本框和樹視圖選擇有一些疑問。因此,在我提出這個問題之前,讓我提供我已經完成的工作的背景。數據綁定分層數據模板和樹視圖

我有我的數據庫中下表:

Table: tbl_Vendors 
Column: ID - Autonumber 
Column: Name - string(75) 

Table: tbl_Departments 
Column: ID - Autonumber 
Column: Name - string(100) 

Table: tbl_Products 
Column: ID - Autonumber 
Column: Department - Number (linked to tbl_Departments.ID) 
Column: Description - string(255) 
Column: UPC - string(12) 
Column: Price - currency 

Table: tbl_Product_Vendor_Cost 
Column: VendorID - number (linked to tbl_Vendors.ID) 
Column: ProductID - number (linked to tbl_Products.ID) 
Column: Cost - Currency 

在我的XAML窗口,我有我使用的層級數據模板與數據工作的一個TreeView,它顯示的產品,通過排序部。下面是我使用的資源:

<Window.Resources> 
     <!-- The data template for the products--> 
     <DataTemplate x:Key="productTemplate"> 
      <TextBlock Text="{Binding Name}"/> 
     </DataTemplate> 

     <!-- The data template for the departments--> 
     <HierarchicalDataTemplate x:Key="treeViewTemplate" ItemsSource="{Binding Department2Product}" ItemTemplate="{StaticResource productTemplate}"> 
      <TextBlock Text="{Binding Name}"/> 
     </HierarchicalDataTemplate> 
    </Window.Resources> 

在窗口的XAML文件,我已經叫了這樣的樹視圖:

<TreeView Name="lstProducts" ItemsSource="{Binding tbl_Departments}" ItemTemplate="{StaticResource treeViewTemplate}" AlternationCount="2" SelectedValuePath="ID"/> 

在窗口的.cs文件我已經定義了以下方法:

Public void UpdateUI() { 
    DataSet outResult; 
    If (ProductsDatabase.TryToGetProductsDataset(out outResult)) { 
     this.tvwProducts.DataContext = outResult; 
    } 
} 

的UpdateUI方法每當改變對設備進行的被調用時,或每當InitializeComponent方法運行爲好。所以,現在這一切都表明,這是我的實際問題。我在窗口上有一些文本框,當我選擇一個產品時,我希望文本框填充選定產品的數據。我想要忽略任何被選中的部門,或者完全阻止部門的實際選擇,無論哪種方式都行得通。我會嘗試一些其他的事情,以及我焦急地等待來自這裏的任何答案。

感謝您的時間,我欣賞任何可以給出的答案。

回答

0

所以,我對此採取了不同的方向。

樹形視圖獲取它自己的DataSet,因爲它是數據上下文。然後我使用相同的數據模板和分層數據模板來顯示信息。然後,在後面的代碼中,我確信,在調用TreeView的SelectedValueChanged事件時,我會檢查所選的DataRowView的DataRow是否來自正確的表。如果是,那麼我從數據庫中提取產品信息,並在運行時將其放入控件的數據上下文中。