0

需要对 wpf 中的组合框项进行小修复。我想显示组合框项或在组合框(文本块)中显示旁边的图像。我无法完成它。有人可以帮我解决吗?

提前致谢

   <ComboBox Name="avilibity" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="25"/>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Image Grid.Column="1" Source="/resources/icon.png" ></Image>
                            <TextBlock FontSize="14" Grid.Column="2" ></TextBlock>
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
                <sys:String>Available</sys:String>
                <sys:String>Offline</sys:String>
                <sys:String>Away</sys:String>
            </ComboBox>
4

2 回答 2

0

您的代码中有几个错误: 1. 行和列从索引 0 开始;2、TextBlock需要设置Text属性,才能显示。

如果您只想显示相同的图标,则此代码将起作用(这可以修复您的 perblom):

<ComboBox Name="avilibity" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1">
   <ComboBox.ItemTemplate>
         <DataTemplate>
              <Grid>
                  <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="25"/>
                      <ColumnDefinition Width="*" />
                   </Grid.ColumnDefinitions>
               <Image Grid.Column="0" Source="/resources/icon.png" ></Image>
                        <TextBlock FontSize="14" Grid.Column="1"  Text="{Binding}"></TextBlock>
               </Grid>
         </DataTemplate>
     </ComboBox.ItemTemplate>
     <sys:String>Available</sys:String>
     <sys:String>Offline</sys:String>
     <sys:String>Away</sys:String>
</ComboBox>

但正如我所见,物品有三种状态。也许您想在显示不同项目时更改图像。

这是我的解决方案:

<Window.Resources>
    <converters:ImageSourceConverter x:Key="ImageSourceConverter" />
</Window.Resources>

<ComboBox Name="avilibity" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1">
   <ComboBox.ItemTemplate>
         <DataTemplate>
              <Grid>
                  <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="25"/>
                      <ColumnDefinition Width="*" />
                   </Grid.ColumnDefinitions>
               <Image Grid.Column="0" Source="{Binding Converter={StaticResource ImageSourceConverter}}" />
                        <TextBlock FontSize="14" Grid.Column="1"  Text="{Binding}"></TextBlock>
               </Grid>
         </DataTemplate>
     </ComboBox.ItemTemplate>
     <sys:String>Available</sys:String>
     <sys:String>Offline</sys:String>
     <sys:String>Away</sys:String>
</ComboBox>

图像源转换器:

public class ImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return new BitmapImage(new Uri("/resources/" + value + ".png",UriKind.RelativeOrAbsolute));
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
于 2013-10-12T15:26:35.317 回答
0

第一个问题是 Grid Column 索引是基于 0 的 - 意味着 Image shold 有 Grid.Column = “0”,TextBlock = “1”。

它应该总是相同的图像吗?考虑将用户定义的对象用作具有绑定的属性。因此,您可以拥有单独的图标。

可能是这里的大师之一可以提供仅限 XAML 的示例......我只能想象背后的代码......

这里有两种可能的解决方案:

C# 中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ListImageWithText
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnNotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    private List<ImageWithText> _objectlist;


    public List<ImageWithText> ObjectList
    {
        get
        {
            return _objectlist;
        }

        set
        {
            _objectlist = value;
            OnNotifyPropertyChanged("ObjectList");
        }

    }

    public MainWindow()
    {
        InitializeComponent();

        ImageWithText iwt;
        this._objectlist = new List<ImageWithText>();

        iwt = new ImageWithText();
        iwt.Image = "App.ico";
        iwt.Text = "Away";
        this._objectlist.Add(iwt);

        iwt = new ImageWithText();
        iwt.Image = "App.ico";
        iwt.Text = "Available";
        this._objectlist.Add(iwt);

        iwt = new ImageWithText();
        iwt.Image = "App.ico";
        iwt.Text = "Offline";
        this._objectlist.Add(iwt);
        OnNotifyPropertyChanged("ObjectList");
    }
}

public class ImageWithText
{
    public string Image { get; set; }
    public string Text { get; set; }
}
}

定义具有两个公共属性的小类,将这些对象的列表显示为属性并在 XAML 中使用绑定

    <ComboBox Name="avilibity"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              Grid.Column="1"
              ItemsSource="{Binding ObjectList, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}">


        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="25"/>
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Image Grid.Column="0" Source="{Binding Image}" />
                    <TextBlock FontSize="14" Grid.Column="1" Text="{Binding Text}"/>
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>

    </ComboBox>

XAML 重复的最简单方法是使用 ComboBox 项,我不喜欢这条路径...

    <ComboBox Name="avilibity"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              Grid.Column="1">

        <ComboBoxItem>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="25"/>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Source="App.ico" ></Image>
                <TextBlock FontSize="14" Grid.Column="1" >Available</TextBlock>
            </Grid>
        </ComboBoxItem>

        <ComboBoxItem>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="25"/>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Source="App.ico" ></Image>
                <TextBlock FontSize="14" Grid.Column="1" >Offline</TextBlock>
            </Grid>
        </ComboBoxItem>

        <ComboBoxItem>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="25"/>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Source="App.ico" ></Image>
                <TextBlock FontSize="14" Grid.Column="1" >Away</TextBlock>
            </Grid>
        </ComboBoxItem>
    </ComboBox>
于 2013-10-12T09:52:58.777 回答