简体   繁体   中英

bind Selected item from Collection by combobox and bind it to image

How can i bind Selected item from Collection by combobox and bind it to 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;
    }
}

User Control: here i init collection by pairs 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: here i try bind image to combobox

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

i see on combobox items: option 1, option 2 but it can not reflected on image: image do not change

Thanks

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

Or this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM