2016-06-30 35 views
0

我正在試圖製作一個程序,該程序可以爲您提供任何對象列表的CRUD界面。這包括:未知類型的CRUD操作

  1. 顯示其所有屬性的列表框

  2. 內插入一個新的對象

  3. 更新對象

  4. 刪除的能力,能力的能力物體

請記住,在編譯時,我不知道我得到了什麼樣的對象。例如,我想爲ListBox的DataTemplate中列出的每個屬性都設置一個TextBlock。那麼,如果我不知道該屬性的名稱,我該如何處理數據綁定?另外,當我不知道屬性名稱時,如何生成插入表單?

最後,是否有可能使用純MVVM模式,而沒有任何代碼隱藏?

感謝

+0

你會如何「給」對象到你的程序? – Clemens

+1

我已投票結束這個問題「太廣泛」。但是,您始終可以設置或綁定ListBox的DisplayMemberPath屬性。 – Clemens

+0

MVVM中的代碼完全正確。它應該只關注UI邏輯。在這種情況下,我建議你從許多第三方控制供應商/開源項目中的一個獲取「Property Editor」控件。屬性編輯器需要一個對象並創建UI以編輯其上的屬性值。這是一種常見的模式,但沒有一種可以嵌入到WPF中。 – Will

回答

0

一個選項:自動換行的PropertyInfo在PropertyInfoViewModel這樣你就可以綁定到它的價值:

class PropertyInfoViewModel 
{ 
    Object CRUDObject { get; set; } 
    PropertyInfo PropertyInfo { get; set; } 

    Object Value { 
     get 
     { 
      return PropertyInfo.GetValue(CRUDObject); 
     } 
     set 
     { 
      PropertyInfo.SetValue(CRUDObject, value); 
     } 
    } 
} 

你可以有一個ObservableCollection在CRUDObjectViewModel,當您創建或更改的CRUD填充它附加到(查看反射,如果被這個困惑)。

使用模板選擇器選擇一個特定的編輯器顯示爲PropertyInfoViewModel:

public class PropertyTypeTemplateSelector : DataTemplateSelector 
{ 
    public DataTemplate BooleanTemplate { get; set; } 
    public DataTemplate GuidTemplate { get; set; } 
    public DataTemplate StringTemplate { get; set; } 

    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
     PropertyInfo propertyInfo = (item as PropertyInfoViewModel).PropertyInfo; 
     if (propertyInfo.PropertyType == typeof(Boolean)) 
     { 
      return BooleanTemplate; 
     } 
     else if (propertyInfo.PropertyType == typeof(Guid)) 
     { 
      return GuidTemplate; 
     } 
     else if (propertyInfo.PropertyType == typeof(String)) 
     { 
      return StringTemplate; 
     } 
     return null; 
    } 
} 

你可以使用這樣的:

<ListBox ItemsSource="{Binding Properties}"> 
     <ListBox.Resources> 
      <DataTemplate x:Key="BooleanTemplate"> 
       <CheckBox Content="{Binding PropertyInfo.Name}" IsChecked="{Binding Value}"/> 
      </DataTemplate> 
      <DataTemplate x:Key="GuidTemplate"> 
       <StackPanel> 
        <TextBox Text="{Binding PropertyInfo.Name}"/> 
        <TextBox Text="{Binding Value, ValueConverter={StaticResources MyGuidConverter}}"/> 
       </StackPanel> 
      </DataTemplate> 
      <DataTemplate x:Key="StringTemplate"> 
       <StackPanel> 
        <TextBox Text="{Binding PropertyInfo.Name}"/> 
        <TextBox Text="{Binding Value}"/> 
       </StackPanel> 
      </DataTemplate> 
      <DataTemplate x:Key="Null"/> 
     </ListBox.Resources> 
     <ListBox.ItemTemplateSelector> 
      <helpers:PropertyTypeTemplateSelector BooleanTemplate="{StaticResource BooleanTemplate}" 
                GuidTemplate="{StaticResource GuidTemplate}" 
                StringTemplate="{StaticResource StringTemplate}"/> 
     </ListBox.ItemTemplateSelector> 
    </ListBox> 

可能要思考如何應對變化/更新,因爲這不是使用NotifyPropertyChanged保持UI最新。

我還沒有測試過任何這個,但它應該工作,我想。

0

此控件WPFCrudControl可能適合您的問題。

基於MVVM模式實現的泛型WPF CrudControl。它爲簡單的CRUD操作(添加,編輯,刪除,驗證,列出排序,分頁和搜索)提供了巨大的生產力提升。該控件抽象化UI和業務邏輯,因此它需要相對較少的編碼工作量,同時可以自定義其行爲。

+1

爲了達到你的答案upvotes你應該把更多的內容。你知道,鏈接可能會在未來的某個時間點被打破;所以直接在你的答案中提供一些內容/報價/ ...總是有幫助的。 – GhostCat

+0

好的,謝謝你,我會編輯它。 –