하나의 옵션 : 당신은 당신이 그것을 만들거나 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 사용하고 있지 않습니다.
나는 이것들을 테스트하지 않았지만 효과가 있다고 생각합니다.
출처
2016-06-30 16:23:02
Joe
개체를 프로그램에 어떻게 "제공"합니까? – Clemens
나는이 질문을 "너무 광범위하다"고 결론 지었다. 그러나 항상 ListBox의 DisplayMemberPath 속성을 설정하거나 바인딩 할 수 있습니다. – Clemens
MVVM에서는 코드 숨김이 완벽합니다. UI 로직에만 관심을 가져야합니다. 이 경우 많은 타사 컨트롤 공급 업체/오픈 소스 프로젝트 중 하나에서 "Property Editor"컨트롤을 가져와야합니다. 속성 편집기는 객체를 가져 와서 속성 값을 편집 할 수있는 UI를 만듭니다. 일반적인 패턴이지만 WPF에 구운 패턴은 없습니다. – Will