C# 选择图像名称时,在表单中显示图像

C# 选择图像名称时,在表单中显示图像,c#,wpf,C#,Wpf,WPF的新成员。当从组合框中选择图像名称时,我希望在表单中显示图像(图像存储在填充组合框的sql数据库中) 有谁知道如何做到这一点的例子吗。我添加了在从combo选择时填充文本框的代码 private void comboBoxDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e) { string constring = "Data Source=tcp:****;Initial

WPF的新成员。当从组合框中选择图像名称时,我希望在表单中显示图像(图像存储在填充组合框的sql数据库中)

有谁知道如何做到这一点的例子吗。我添加了在从combo选择时填充文本框的代码

   private void comboBoxDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {
        string constring = "Data Source=tcp:****;Initial   Catalog=******;Persist Security Info=True;User ID=*******;Password=******";

        string Query = "select * from tables where Name='" +  comboBoxDisplay.SelectedItem.ToString() + "' ;";
        SqlConnection conDataBase = new SqlConnection(constring);
        SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
        SqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {

                string sReid = myReader.GetInt32(0).ToString();
                string sName = myReader.GetString(1);
                string sPicture = myReader.GetString(3);

                txtReId.Text = sReid;
                txtName.Text = sName;
                txtPicture.Text = sPicture;

            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

通常,该网站的用户更喜欢问题作者提供更多的信息。我们也希望看到你至少自己尝试过寻找答案

但是,由于您是新用户,我将为您提供完整的解决方案。有几种方法可以实现这一点。。。我将向您展示一种方法,您可以根据自己的需要进行调整

启动一个新的WPF项目,并将以下内容添加到
MainWindow.xaml.cs
文件中:

using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Images.Add(new Tuple<string, string>("Picture 1", 
"/WpfApplication1;component/Images/Picture 1.png"));
            Images.Add(new Tuple<string, string>("Picture 2", 
"/WpfApplication1;component/Images/Picture 2.png"));
            Images.Add(new Tuple<string, string>("Picture 3", 
"/WpfApplication1;component/Images/Picture 3.png"));
        }

        public static DependencyProperty ImagesProperty = DependencyProperty.Register(
"Images", typeof(ObservableCollection<Tuple<string, string>>), typeof(MainWindow), 
new PropertyMetadata(new ObservableCollection<Tuple<string, string>>()));

        public ObservableCollection<Tuple<string, string>> Images
        {
            get { return (ObservableCollection<Tuple<string, string>>)GetValue(
ImagesProperty); }
            set { SetValue(ImagesProperty, value); }
        }
    }
}
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>
        <Image Grid.Row="0" Source="{Binding SelectedValue, 
ElementName=ImageComboBox}" />
        <ComboBox Grid.Row="1" Name="ImageComboBox" ItemsSource="{Binding Images}" 
DisplayMemberPath="Item1" SelectedValuePath="Item2" Height="23" Width="120" />
    </Grid>
</Window>
这里我将
Images
集合绑定到
ComboBox.ItemsSource
属性,并将
displaymberpath
属性设置为
Item1
。。。这是保存图片名称的第一个
元组
属性的名称-因此,将显示名称。我还将
SelectedValuePath
属性设置为
Item2
。。。这是保存图片文件路径的第二个
元组
属性的名称-因此,所选项目的值将是所选
图像
的文件路径

还要注意,
Image.Source
属性设置为
Binding SelectedValue,ElementName=ImageComboBox
。这意味着图片的来源将来自
组合框。SelectedValue
,如果您记得的话,它被设置为所选图像的文件路径

注意事项:

当然,您可以用自己的类替换
元组
对象,只要您更新用于
组合框.SelectedValuePath
组合框.DisplayMemberPath
属性的正确属性的名称


还请将与此示例代码一起使用的任何图像添加到应用程序根目录中名为
images
的文件夹中。此外,除非您将它们命名为
Picture 1.png
Picture 2.png
Picture 3.png
,否则您需要更新添加到
Images
集合中的文件路径。

如图所示,这可以完全在Xaml中完成,并使用大约20行代码的IValueConverter。要获得最佳答案,您应该编辑您的问题,以显示用于填充组合框的数据类型,以及是否需要在每次选择更改时查询数据库。谢谢我进行了编辑。@user2631662,如果您喜欢Sheridan的答案,请使用“接受”按钮进行标记。这样做会告诉其他用户他的答案有效;也有助于吸引他们对你将来可能遇到的任何问题的兴趣,因为这表明你感谢他们的意见。