2016-07-25 72 views

回答

0

这里是你如何绑定组合框里面的数据 -

<ComboBox ItemsSource="{Binding SomeCollection}"> 

     <ComboBox.ItemTemplate> 

      <DataTemplate> 

       <StackPanel Orientation="Horizontal"> 

        <TextBlock Text="{Binding Key}" 
           Margin="5"/> 
        <TextBlock Text="{Binding Value}" 
           Margin="5"/> 

       </StackPanel>         

      </DataTemplate> 

     </ComboBox.ItemTemplate> 

    </ComboBox> 

风格它,你在DataTemplate中希望

0

达到你想要什么,你必须确保你的ViewModel兼有KeyCollection和字典。在这里我解释:

假设你有这样的视图模型:

public class DictVm 
    { 
     public Dictionary<string, string> MainDictionary { get; set; } 
     public ObservableCollection<string> MyKeyCollection{ get; set; } 
     public string SelectedKey{ get; set; } 

     private string _selectedDictValue; 
     public string SelectedDictValue { 
     get { 

      if (MainDictionary.TryGetValue(SelectedKey, _selectedDictValue)) 
       return _selectedDictValue; 
      return string.Empty; 
     } 
     set { _selectedDictValue = value; } } 
    } 

这里是你的主窗口构造器:

private DictVm vm; 
    public MainWindow() 
    { 
     InitializeComponent(); 

     vm = new DictVm(); 
     DataContext = vm; 
    } 

这里我怎么会修改XAML:

<ComboBox x:Name="MyCombo" ItemsSource="{Binding MyKeyCollection}" SelectedValue="{Binding SelectedKey}"/> 

<TextBox Text="{Binding SelectedDictValue}"/>