通过组合框绑定Collection中的选定项并将其绑定到图像

时间:2016-09-29 15:03:13

标签: c# wpf

如何通过combobox绑定Collection中的Selected项并将其绑定到image?

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但它无法反映在图像上:图像不会改变

由于

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

或者这个:

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