2016-09-29 86 views
0

我如何通過組合框綁定選定項目並將其綁定到圖像?從組合框中綁定選定的項目並將其綁定到圖像

public class ElectrodePlacementScheme 
{ 
    public BitmapImage Image { private set; get; } 
    public String Name { private set; get; } 

    public ElectrodePlacementScheme(BitmapImage image, String name) 
    { 
     Image = image; 
     Name = name; 
    } 
} 

用戶控制:在這裏,我初始化集合成對的名義和形象

public partial class CheckECGUC : UserControl 
{ 
    public ObservableCollection<ElectrodePlacementScheme> ElectrodePlacementSchemes { get; set; } 

    public CheckECGUC() 
    { 
     InitializeComponent(); 

     ElectrodePlacementSchemes = new ObservableCollection<ElectrodePlacementScheme>(); 
     ElectrodePlacementSchemes.Add(new ElectrodePlacementScheme(new BitmapImage(new System.Uri(@"pack://application:,,,/Images/3CH_7Leads_Option1.png")), "Option 1")); 
     ElectrodePlacementSchemes.Add(new ElectrodePlacementScheme(new BitmapImage(new System.Uri(@"pack://application:,,,/Images/3CH_7Leads_Option2.png")), "Option 2")); 
    } 
} 

XAML的:在這裏,我嘗試綁定圖像組合框

<ComboBox x:Name="optionSelector" ItemsSource="{Binding ElectrodePlacementSchemes}" DisplayMemberPath="Name"/> 
    <Image Source="{Binding Path=optionSelector, ElementName=SelectedItem}"/> 
</ComboBox> 

我對組合框項目看:選項1,選項2但它不能反映在圖像上:圖像不改變

謝謝

回答

0
<ComboBox x:Name="optionSelector" ItemsSource="{Binding ElectrodePlacementSchemes}" DisplayMemberPath="Name"/> 
       <Image Source="{Binding SelectedItem.Image, ElementName=optionSelector}"> 
+0

嗯,是的。但是這個答案需要更好的格式。 –

0

或者這樣:

<ComboBox x:Name="optionSelector" 
       DisplayMemberPath="Name" 
       ItemsSource="{Binding ElectrodePlacementSchemes}" 
       SelectedValuePath="Image" /> 
    <Image Source="{Binding Path=SelectedValue, ElementName=optionSelector}" />