简体   繁体   English

Windows Phone 8.1 ComboBox样式图像

[英]Windows phone 8.1 ComboBox style Image

I want to include an image directly into a ComboBox template. 我想将图像直接包含在ComboBox模板中。

I found this part of the code and I believe it is here where I would put it: 我找到了代码的这一部分,我相信这里是我要放置的部分:

<Button x:Name="FlyoutButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" FontWeight="Normal" FlowDirection="{TemplateBinding FlowDirection}" FontSize="{ThemeResource ContentControlFontSize}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" MinHeight="{ThemeResource ComboBoxItemMinHeightThemeSize}" Padding="6.5,0,0,0" Grid.Row="1">
   <ContentPresenter x:Name="ContentPresenter" Margin="0,0.8,0,0" MinHeight="32.5">
       <TextBlock x:Name="PlaceholderTextBlock" Margin="0" Style="{StaticResource ComboBoxPlaceholderTextBlockStyle}" Text="{TemplateBinding PlaceholderText}"/>
   </ContentPresenter>
</Button>

I can't put an Image inside contentPresenter because it says I can only set 'Content' once. 我不能将Image放入contentPresenter内,因为它说我只能设置一次'Content'。

If I make something like: 如果我做类似的事情:

<ContentPresenter x:Name="ContentPresenter" Margin="0,0.8,0,0" MinHeight="32.5">
   <StackPanel Orientation="Horizontal">
       <TextBlock x:Name="PlaceholderTextBlock" Margin="0" Style="{StaticResource ComboBoxPlaceholderTextBlockStyle}" Text="{TemplateBinding PlaceholderText}"/>
       <Image Source="ms-appx:///Assets/Arrow.png" />
   </StackPanel>
</ContentPresenter>

It actually works but I get an error in my XAML view page: "No installed components were detected. Cannot resolve TargetName PlaceholderTextlock.". 它实际上可以正常工作,但在XAML视图页面中出现错误:“未检测到已安装的组件。无法解析TargetName PlaceholderTextlock。”。 And also the image disappears after I select an item. 而且,选择项目后图像也会消失。

I'd appreciate some guidance. 我希望得到一些指导。

I believe you want to set the items inside the combo box to have the image with the textblock. 我相信您希望在组合框中设置项目以使图像带有文本块。 In that case you need to set the ItemTemplate of the combobox like this 在这种情况下,您需要像这样设置组合框的ItemTemplate

<ComboBox Name="hik" ItemTemplate="{StaticResource cmbx}">

And in your page resources you can define the item template for combo box items like this 在页面资源中,您可以为组合框项目定义项目模板,如下所示

<DataTemplate x:Key="cmbx">
    <StackPanel Orientation="Horizontal" Background="Aqua">
        <TextBlock HorizontalAlignment="Left" Margin="0,0,0,0" Foreground="Black"  TextWrapping="Wrap" Text="Some Text" VerticalAlignment="Top"/>
        <Image Source="/Assets/1.png" Stretch="Uniform" Height="100" Width="100" />
    </StackPanel>
</DataTemplate>

Once that is done when you run it on clicking the combobox you can see the list with image 一旦单击组合框运行它,就可以看到带有图像的列表

在此处输入图片说明

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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