通过组合框从集合中绑定选定的项目,并将其绑定到图像

本文关键字:绑定 项目 图像 组合 集合 | 更新日期: 2023-09-27 18:09:25

如何通过组合框从集合中绑定选定项目并将其绑定到图像?

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;
    }
}

User Control: here i init collection by pair of name and image

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

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

我看到组合框项目:选项1,选项2但它不能反映在图像上:image do not change

谢谢

通过组合框从集合中绑定选定的项目,并将其绑定到图像

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

或者

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