UserControl을 만들려고합니다. 그러면 입력란 Dictionary<string,string>
의 사전을 편집 할 수 있습니다 (항목을 지금까지 편집 중이거나 추가 또는 삭제하지 않음). 나는 사전에 데이터 그리드를 결합 할 때마다DataGrid가 변경 될 때 내 ViewModel의 속성이 업데이트되지 않는 이유는 무엇입니까?
그것은 읽기 전용으로 그리드를 보여줍니다, 그래서 나는 DictionaryEntry
이 두 가지 속성 Key
및 Value
와 단지 클래스 곳이 ObservableCollection<DictionaryEntry>
로 변환 것, 값 변환기를 만들 수 decieded.
이것은 표를 사전에 표시하기 위해 작동하지만, 이제 표를 변경할 때 사전이 업데이트되지 않습니다. 나는 왜 그런지 확신 할 수 없다.
내 바인딩 설정 방식이나 가치 변환기에 문제가 있다고 생각합니다. 누군가가 약간의 빛을 비출 수 있다면 그것은 환상적 일 것입니다.
다음은 제가 할 수있는 가장 작은 데모입니다. 다시 문제는 내가 그리드의 값을 변경할 때 에있는 MyDictionary
이 업데이트되지 않습니다. 왜?
MainViewModel.cs
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
_myDictionary = new Dictionary<string, string>()
{
{"Key1", "Value1"},
{"Key2", "Value2"},
{"Key3", "Value3"}
};
}
private Dictionary<string, string> _myDictionary;
public Dictionary<string, string> MyDictionary
{
get
{
return _myDictionary;
}
set
{
if (_myDictionary == value)
return;
_myDictionary = value;
OnPropertyChanged("MyDictionary");
}
}
...
}
MainWindow.xaml
<Window ...>
<Window.Resources>
<local:MainViewModel x:Key="MainViewModel"></local:MainViewModel>
</Window.Resources>
<StackPanel Name="MainStackPanel" DataContext="{Binding Source={StaticResource MainViewModel}}">
<local:DictionaryGrid />
<Button Content="Print Dictionary" Click="PrintDictionary"></Button>
</StackPanel>
</Window>
DictionaryGrid.xaml
<UserControl ...>
<UserControl.Resources>
<testingGrid:DictionaryToOcConverter x:Key="Converter" />
</UserControl.Resources>
<Grid>
<DataGrid ItemsSource="{Binding MyDictionary,
Converter={StaticResource Converter}}"
/>
</Grid>
</UserControl>
DictionaryToOcConverter.cs
public class DictionaryToOcConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var collection = new ObservableCollection<DictionaryEntry>();
var dictionary = value as Dictionary<string, string>;
if (dictionary != null)
{
foreach (var kvp in dictionary)
collection.Add(new DictionaryEntry { Key = kvp.Key, Value = kvp.Value });
}
return collection;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var dictionary = new Dictionary<string, string>();
var entries = value as ObservableCollection<DictionaryEntry>;
if (entries != null)
{
foreach (var entry in entries)
dictionary.Add(entry.Key, entry.Value);
}
return dictionary;
}
public class DictionaryEntry
{
public string Key { get; set; }
public string Value { get; set; }
}
}
감사합니다. 나는 이러한 변화를 만들었다. 내부 사전 변경 스틱,하지만 그들은 내 기본보기 모델로 돌아 오지 않아, 내가 IValueConverter 통해 돌아갈 수 있도록 할 일이 없는가? ConvertBack 메서드는 절대로 호출되지 않으며 observable 컬렉션이 변경 될 때 호출되어야하는지 또는 다른 것을 연결해야합니까? –
나는 그것을 내 대답에 포함하고 싶지 않았지만 값 변환기는 꽤 많은 청어이다. 나는 그것을 비울 것이다. 변경 사항이 작동하면 (그리고해야 함), 추가 추상화 계층에서 알림을 원하면 VM은 각 DictionaryEntry의 속성을 변경하고 해당 이벤트를 처리하기 위해 단일 이벤트 핸들러를 구현해야합니다. 또는 VM의 항목 소스로 '내부 사전'을 사용하십시오. –