基于WPF

时间:2016-01-01 13:06:15

标签: c# wpf data-binding combobox

我在下面的差异位置有3个组合框,每个组合框下有3个图像。

<ComboBox Name="Category1" Grid.Row="0" Grid.Column="0"
        HorizontalAlignment="Left"
        Margin="40,20,0,0"
        VerticalAlignment="Top"
        Width="120"
        Loaded="Category1_Loaded"
        SelectionChanged="Category1_SelectionChanged"/>
        <Image Width="120" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" Margin="40,40,0,0" Name="CB1" Source="C:\Users\Image1.png" />

        <ComboBox Name="Category2" Grid.Row="0" Grid.Column="1"
        HorizontalAlignment="Left"
        Margin="40,20,0,0"
        VerticalAlignment="Top"
        Width="120"
        Loaded="Category2_Loaded"
        SelectionChanged="Category2_SelectionChanged"/>
        <Image Width="120" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Margin="40,40,0,0" Name="CB2" Source="C:\Users\Image2.png" />

        <ComboBox Name="Category3" Grid.Row="0" Grid.Column="2"
        HorizontalAlignment="Left"
        Margin="40,20,0,0"
        VerticalAlignment="Top"
        Width="120"
        Loaded="Category3_Loaded"
        SelectionChanged="Category3_SelectionChanged"/>
        <Image Width="120" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Left" Margin="40,40,0,0" Name="CB3" Source="C:\Users\Image3.png" />

和.cs代码

 private void Category1_Loaded(object sender, RoutedEventArgs e)
        {
            // ... A List.
            List<string> data = new List<string>();
            data.Add("Category1_1");
            data.Add("Category1_2");
            // ... Get the ComboBox reference.
            var comboBox = sender as ComboBox;
            // ... Assign the ItemsSource to the List.
            comboBox.ItemsSource = data;
            // ... Make the first item selected.
            comboBox.SelectedIndex = 0;
        }

private void Category2_Loaded(object sender, RoutedEventArgs e)
        {
            // ... A List.
            List<string> data = new List<string>();
            data.Add("Category2_1");
            data.Add("Category2_2");
            // ... Get the ComboBox reference.
            var comboBox = sender as ComboBox;
            // ... Assign the ItemsSource to the List.
            comboBox.ItemsSource = data;
            // ... Make the first item selected.
            comboBox.SelectedIndex = 0;
        }
private void Category3_Loaded(object sender, RoutedEventArgs e)
        {
            // ... A List.
            List<string> data = new List<string>();
            data.Add("Category3_1");
            data.Add("Category3_2");
            // ... Get the ComboBox reference.
            var comboBox = sender as ComboBox;
            // ... Assign the ItemsSource to the List.
            comboBox.ItemsSource = data;
            // ... Make the first item selected.
            comboBox.SelectedIndex = 0;
        }

现在有3个组合框和图片,如下所示 enter image description here

  

我想根据下拉列表更改或显示特定图像   选择。例如,如果我选择下拉值Category1_1   应该给我看图像c:\users\Category1_1.PNG

2 个答案:

答案 0 :(得分:1)

我现在无法测试它,但从MSDN中获取一些示例,您选择的组合框类别1的更改事件应该是这样的

    private void Category1_SelectionChanged(object sender, RoutedEventArgs e)
    {
        var comboBox = sender as ComboBox;

        // Check if you have something selected (this happens sometime)
        if(comboBox.SelectedIndex != -1)
        {
            // Take the text of the combo and build the path to the file
            string fileName = Path.Combine(@"C:\users", comboBox.Text + ".png"); 

            // Again, check if we really have that file available
            if(File.Exists(fileName))
            {
                // Build a BitmapImage from the file
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri(fileName, UriKind.Relative);
                bi.EndInit();

                // Set the Image for this combo. Not sure if the Stretch part is needed
                CB1.Stretch = Stretch.Fill;
                CB1.Source = bi3;
            } 
        }
    }

来自MSDN的引用

Image.Source property
ComboBox.Text Property

另外,我不确定读取Text属性是否按预期工作(由ComboBox IsEditable属性引起的可能问题)。在任何情况下,您都可以使用SelectedItem.ToString()

如果你为组合Category1做了这个工作,那么这很容易被重构为通用组合的所有三个组合的通用方法来源于事件和要更新的图像

答案 1 :(得分:1)

您可以使用将类别字符串转换为BitmapImage的绑定转换器将Image的Source属性直接绑定到相应ComboBox的SelectedItem

<Window.Resources>
    <local:ImageConverter x:Key="ImageConverter"/>
</Window.Resources>
...
<ComboBox x:Name="comboBox1" .../>
<Image Source="{Binding SelectedItem, ElementName=comboBox1,
                        Converter={StaticResource ImageConverter}}"/>

转换器看起来像这样:

public class ImageConverter : IValueConverter
{
    public object Convert(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            var path = string.Format(@"C:\Users\{0}.png", value);
            return new BitmapImage(new Uri(path));
        }
        catch (Exception ex)
        {
            return null; // or some default image
        }
    }

    public object ConvertBack(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

作为一个注释,您可以在XAML中轻松创建ComboBox项目列表,如下所示:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
...
<ComboBox x:Name="comboBox1" SelectedIndex="0" ...>
    <ComboBox.Items>
        <sys:String>Category1_1</sys:String>
        <sys:String>Category1_2</sys:String>
        <sys:String>Category1_3</sys:String>
    </ComboBox.Items>
</ComboBox>