2016-06-30 3 views
0

제공하는 모든 개체의 목록에 대해 CRUD 인터페이스를 제공하는 프로그램을 만들려고합니다. 즉 포함알 수없는 유형의 CRUD 작업

  1. 가옵니다

  2. 내부에 새로운 객체

  3. 에게 객체

  4. 를 삭제하는 기능을 업데이트 할 수있는 기능을 삽입 할 수있는 능력을 자신의 모든 속성을보기 대상물

컴파일 타임에 나는 어떤 종류의 객체를 얻는 지 전혀 모릅니다. 예를 들어 ListBox의 DataTemplate 내에 나열된 각 속성에 대해 TextBlock을 갖고 싶습니다. 그래서 속성의 이름을 모르는 경우 데이터 바인딩을 어떻게해야합니까? 또한 속성 이름을 모르는 경우 삽입 양식을 어떻게 생성합니까?

마지막으로, 코드 비하인드없이 순수 MVVM Pattern을 사용하여 수행 할 수 있습니까?

감사

+0

개체를 프로그램에 어떻게 "제공"합니까? – Clemens

+1

나는이 질문을 "너무 광범위하다"고 결론 지었다. 그러나 항상 ListBox의 DisplayMemberPath 속성을 설정하거나 바인딩 할 수 있습니다. – Clemens

+0

MVVM에서는 코드 숨김이 완벽합니다. UI 로직에만 관심을 가져야합니다. 이 경우 많은 타사 컨트롤 공급 업체/오픈 소스 프로젝트 중 하나에서 "Property Editor"컨트롤을 가져와야합니다. 속성 편집기는 객체를 가져 와서 속성 값을 편집 할 수있는 UI를 만듭니다. 일반적인 패턴이지만 WPF에 구운 패턴은 없습니다. – Will

답변

0

하나의 옵션 : 당신은 당신이 그것을 만들거나 CRUD를 변경할 때 채워 당신의 CRUDObjectViewModel,에 ObservableCollection에있을 수 있었다

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

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

: 그것은 가치의에 랩 PropertyInfo PropertyInfoViewModel에서 당신은 바인딩 할 수 있도록 그것은 (이것에 의해 혼란스러워한다면 반사를 찾는다)에 붙어있다.

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

변경에 대처하는 방법에 대해 생각해야 할 수도 있습니다/업데이트, UI를 최신 상태로 유지하기 위해 NotifyPropertyChanged 사용하고 있지 않습니다.

나는 이것들을 테스트하지 않았지만 효과가 있다고 생각합니다.

0

이 컨트롤 WPFCrudControl이 문제가 될 수 있습니다.

MVVM 패턴을 기반으로 구현 된 일반 WPF CrudControl입니다. 간단한 CRUD 작업 (추가, 편집, 삭제, 유효성 검사, 정렬, 페이징 및 검색을 통한 목록 작성)을 위해 엄청난 생산성 향상을 제공합니다. 컨트롤은 UI와 비즈니스 로직을 모두 추상화하므로 상대적으로 최소한의 코딩 작업 만 필요로하면서 동작을 사용자 정의 할 수 있습니다.

+1

답변에 대한 상향식을 얻기 위해서는 좀 더 많은 내용을 넣어야합니다.링크는 미래의 어떤 시점에서 깨질 수 있습니다. 그래서 당신의 대답 안에서 직접 내용/견적/...을주는 것이 도움이됩니다. – GhostCat

+0

오케이, 고맙습니다. 편집하겠습니다. –

관련 문제