이것은 약간 해키지만, 나는 그것이 당신이 원하는 것을 이해한다고 가정 할 때 작동하도록했습니다.XAML에서
class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
this.data.Add(1, "One");
this.data.Add(2, "Two");
this.data.Add(3, "Three");
}
Dictionary<int, string> data = new Dictionary<int, string>();
public IDictionary<int, string> Data
{
get { return this.data; }
}
private KeyValuePair<int, string>? selectedKey = null;
public KeyValuePair<int, string>? SelectedKey
{
get { return this.selectedKey; }
set
{
this.selectedKey = value;
this.OnPropertyChanged("SelectedKey");
this.OnPropertyChanged("SelectedValue");
}
}
public string SelectedValue
{
get
{
if(null == this.SelectedKey)
{
return string.Empty;
}
return this.data[this.SelectedKey.Value.Key];
}
set
{
this.data[this.SelectedKey.Value.Key] = value;
this.OnPropertyChanged("SelectedValue");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
var eh = this.PropertyChanged;
if(null != eh)
{
eh(this, new PropertyChangedEventArgs(propName));
}
}
}
그리고 :
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox x:Name="ItemsListBox" Grid.Row="0"
ItemsSource="{Binding Path=Data}"
DisplayMemberPath="Key"
SelectedItem="{Binding Path=SelectedKey}">
</ListBox>
<TextBox Grid.Row="1"
Text="{Binding Path=SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
Data
속성의 값이 ListBox
의 ItemsSource
에 바인딩
내가 먼저 뷰 모델 클래스를 만들었습니다. 질문을 올리면 의 인스턴스가
ListBox
뒤에있는 데이터로 사용됩니다.
DisplayMemberPath
을
Key
으로 설정하여 키 값이
ListBox
의 각 항목에 대해 표시된 값으로 사용됩니다. 당신이 발견 즉 읽기 전용되기 때문에
, 당신은 단지의 TextBox
에 대한 데이터로 KeyValuePair
의 Value
을 사용할 수 없습니다. 대신 TextBox
은보기 모델의 속성에 바인딩되어 현재 선택된 키의 값을 가져 와서 설정할 수 있습니다. SelectedItem
속성 (ListBox
)을보기 모델의 다른 속성에 바인딩하여 업데이트됩니다. 이 속성을 nullable (KeyValuePair
는 구조체이기 때문에)로 만들어야 만 선택 사항이 없을 때 코드가 감지 할 수있었습니다.
내 테스트 앱에서는보기 모델의 Dictionary
으로 전파되는 TextBox
을 수정 한 것으로 보입니다.
당신이 가고있는 것이 더 많거나 적습니까? 조금 깨끗해야 할 것 같지만 그렇게 할 방법이 있는지 확실하지 않습니다. 대신
Dictionary<string, int>
의
의 자신의 버전을 구현할 수있다? – Rauhotz
어떻게 그것을 두 가지 방식으로 만들려고합니까? 값이 TextBox에 표시됩니까? – Ray
@ 레이 내 값을위한 사용자 지정 편집기가 있습니다. TextBox를 사용한 예가 있다면, 필자의 요구를 충족시킬 수있을 것이라고 확신합니다. –