【问题标题】:bind Selected item from Collection by combobox and bind it to image通过组合框从集合中绑定所选项目并将其绑定到图像
【发布时间】:2016-09-29 15:03:12
【问题描述】:

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

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

谢谢

【问题讨论】:

    标签: c# wpf


    【解决方案1】:
    <ComboBox x:Name="optionSelector" ItemsSource="{Binding ElectrodePlacementSchemes}" DisplayMemberPath="Name"/>
                    <Image Source="{Binding SelectedItem.Image, ElementName=optionSelector}">
    

    【讨论】:

    • 嗯,是的。但是这个答案需要更好的格式。
    【解决方案2】:

    或者这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2011-11-17
      • 1970-01-01
      • 2014-08-15
      • 2016-08-20
      相关资源
      最近更新 更多