0

I am working on an existing code. This thing has got a ComboBox with a few ComboBoxItems. Each items has got a StackPanel withing which there is an Image control and a TextBlock.

Now the source property of the Image control is set to different vector images stored in XAML files whereas the Text property of the TextBlock is set to a localized string.

I want to implement this not by having separate ComboBoxItems but by using DataTemplate. I can create a List of the strings for the TextBlock but I am not able to figure out as to how can I bind the images to the respective Image Controls.

I am open to any other better possible solution. Also, if you think that the right way to do it is the existing one, please let me know.

This might be a duplicate question but I couldn't find one that caters to my issue. If it is, a link to the other question would suffice.

EDIT: Code Added

<ComboBox x:Name="imageInfoLevelsComboBox" SelectedIndex="1" 
          Style="{DynamicResource ComboBoxToolBarStyle}" 
          Margin="6,6,6,0" Width="50" 
          ToolTip="{x:Static Viewing:ViewingTexts.ImageInformationLevels}" 
          SelectionChanged="OnImageInfoLevelsComboBoxSelectionChanged" >
    <ComboBoxItem x:Name="showAllComboBoxItem" 
                  Style="{DynamicResource ComboBoxItemToolBarStyle}">
        <StackPanel Orientation="Horizontal">
            <Image x:Name="ImageInfoAllImage" 
                   Source="{StaticResource ImageInfoFullIcon}" 
                   Margin="0,0,4,0" 
                   Width="24" Height="24"/>
            <TextBlock 
                Text="{x:Static Viewing:ViewingTexts.ImageInformationFull}"
                Margin="10,0,0,0" 
                VerticalAlignment="Center"/>
        </StackPanel>
    </ComboBoxItem>
    <ComboBoxItem x:Name="showImportantComboBoxItem" 
        Style="{DynamicResource ComboBoxItemToolBarStyle}">
        <StackPanel Orientation="Horizontal">
            <Image x:Name="ImageInfoImportantImage" 
                   Source="{StaticResource ImageInfoLimitedIcon}" 
                   Margin="0,0,4,0" 
                   Width="24" Height="24"/>
            <TextBlock 
                Text="{x:Static Viewing:ViewingTexts.ImageInformationIntermediate}"
                       Margin="10,0,0,0" 
                       VerticalAlignment="Center"/>
        </StackPanel>
    </ComboBoxItem>
    <ComboBoxItem x:Name="showNotificationsComboBoxItem" 
        Style="{DynamicResource ComboBoxItemToolBarStyle}">
        <StackPanel Orientation="Horizontal">
            <Image x:Name="ImageInfoNotificationsImage" 
                Source="{StaticResource ImageInfoNoneIcon}" 
                Margin="0,0,4,0" Width="24" Height="24"/>
            <TextBlock Text="{x:Static Viewing:ViewingTexts.ImageInformationNone}"
                       Margin="10,0,0,0" VerticalAlignment="Center"/>
        </StackPanel>
    </ComboBoxItem>
</ComboBox>

What I think I can do is create a class with 2 objects, one of type string and the other as Image. And then create a list and bind it with the combobox, but the issue is that I am nto sure how to use the vector image as an object.

Thanks.

4

2 回答 2

1

将 ComboBox 的 ItemsSource 绑定到具有表示文本和图像的属性的对象集合。然后,您需要创建一个 IValueConverter 并在您的图像绑定上指定它的一个实例,该实例可以将您对象上的属性值转换为图像源。

这是一个例子: http: //www.codewrecks.com/blog/index.php/2010/07/23/bind-an-image-to-a-property-in-wpf/

于 2013-02-28T12:03:51.120 回答
1

我认为您需要绑定具有至少两个属性的对象列表,而不仅仅是字符串列表。一个属性将包含文本块的字符串,另一个属性将是图像的 urisource。

有关 urisource 定义的示例,请参阅此链接Wpf - 相对图像源路径

于 2013-02-28T11:22:00.503 回答