【问题标题】:How to add an image and some more text to a group description?如何在组描述中添加图像和更多文本?
【发布时间】:2021-06-10 11:45:35
【问题描述】:

如何在群组描述中添加图片,并在其旁边添加更多文字?

这是我现在拥有的:

我希望徽标位于说明旁边,并希望在蓝色标记处添加一些不同的文字,例如法拉利的“5x108”和道奇的“5x114.3”。但那是存储在它旁边的表格中

我的所有数据都存储在 ms sql 中,这是我检索它的方法:

string connStrings = ConfigurationManager.AppSettings["Sql"];
    string Data = @"Select ps.Mærket AS Mærke, P.DataID, P.Billed, P.Model, P.Årgang, P.[Motor Type], P.Krydsmål, P.Centerhul, P.ET,P.Bolter, P.Dæk, P.Fælge from Data.Hjuldata P  inner join Data.Mærke PS on P.MærkeID = PS.MærkeID ORDER BY ps.Mærket";
public MainWindow()
{
    InitializeComponent();

    BindData();

    ICollectionView dataView = CollectionViewSource.GetDefaultView(hjuldata.ItemsSource);
    dataView.GroupDescriptions.Add(new PropertyGroupDescription("Mærke"));
}

private void BindData()
{
    hjuldata.Items.Clear();
    hjuldata.ItemsSource = kategori().Tables[0].DefaultView;
}

public DataSet kategori()
{
    //SQL statement to fetch entries from data
    DataSet dsdata = new DataSet();

    //Open SQL Connection
    using (SqlConnection conns = new SqlConnection(connStrings))
    {
        conns.Open();

        //Initialize command object
        using (SqlCommand cmds = new SqlCommand(Data, conns))
        {
            SqlDataAdapter adapters = new SqlDataAdapter(cmds);

            //Fill the result set
            adapters.Fill(dsdata);
        }
    }

    return dsdata;
}

我在 XAML 中的尝试

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Expander IsExpanded="False"  BorderBrush="#FFEAEAEA" BorderThickness="0,0,0,1">
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Bottom"/>
                                        <Image Source="{Binding Path=logo}" Width="20" Height="20" Stretch="Fill" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0"/>
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="#FFEAEAEA" FontSize="22" VerticalAlignment="Bottom"/>
                                        <TextBlock Text="{Binding text}" FontWeight="Bold" Foreground="#FFEAEAEA" FontSize="22" VerticalAlignment="Bottom"/>
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ListView.GroupStyle>

我需要做什么才能使我的组标题包含一些详细数据?更改我的 XAML、更改我的对象模型等。

【问题讨论】:

  • 我已经编辑了您的问题,以尝试解决缺少代码格式、标点符号和语法的问题,但我仍然不清楚您的实际问题是什么。您发布的代码是否有效?如果没有,你能澄清一下你需要修复什么帮助吗?也许一张关于它现在的图片以及您想要的图片会有所帮助。
  • 不,因为我不知道如何将它们绑定在那里,但我用图片更新了它,因为我不知道如何更好地描述我的问题
  • 谢谢,添加图片真的很有帮助。我认为您的问题是您试图将行详细信息数据放在组标题中。例如,您按“Dodge”分组,以便数据可以轻松进入标题。但是,徽标、名称和文本都是单个记录的一部分,并且它们在每个“Dodge”行之间可能不同。如果这里不需要分组,我建议去掉 ListBox 上的分组,而是覆盖 ListBoxItem.Template 属性以包含扩展器和数据网格。
  • 实际上分组是这个列表视图中最重要的部分,因为它将包含超过 15.000 种车型,这些车型将被放入 125 个汽车品牌中,当我使用枚举时我能够做文本,但那是只需为螺栓图案使用大量空间(4x100 5x100 5x114.3 6x139.7 等),我可能也可以为这个做这个,所以在汽车品牌旁边添加徽标不是你可以做的吗?
  • 我可能会重新排列我的数据模型以匹配我想要的显示方式。例如,CarBrandModel 可能包含 Brand、Logo、BoltPattern 和 CarModel 的集合,而 CarModel 将包含带有详细数据的实际行。或者,如果您想保留现有的平面数据结构,您可以将字符串 Brand 更改为自定义对象,例如 CarBrandModel,其中将包含名称、徽标和 BoltPattern,然后在该对象上应用分组。不过,我没有足够的汽车知识或您正在做什么,无法真正了解您的数据应该是什么样子。

标签: c# wpf listview grouping


【解决方案1】:

没有数据上下文:

    <Expander.Header>
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Bottom"/>
        <Image Source="{Binding Items[0].logo}" Width="20" Height="20" Stretch="Fill" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0"/>
       .....
      </StackPanel>
    </Expander.Header>

【讨论】:

    【解决方案2】:

    这解决了它

    <StackPanel Orientation="Horizontal" DataContext="{Binding Items}">
    <Image Source="{Binding Path= Billed}" Width="20" Height="20" Stretch="Fill" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0"/>
    <TextBlock Text="{Binding Path=Mærke}" FontWeight="Bold" Foreground="#FFEAEAEA" FontSize="22" VerticalAlignment="Bottom"/>
    <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Bottom"/>
    <TextBlock Text="{Binding Path=Krydsmål}" FontWeight="Bold" Foreground="#FFEAEAEA" FontSize="22" VerticalAlignment="Bottom"/>
    </StackPanel>
    

    【讨论】:

    • 很高兴你明白了!不过请注意 - 您可能必须更改 ItemCount 绑定上的 Source 属性以引用父 DataContext ...我不记得是否有简单的 TemplatedParent 快捷方式,或者使用 AncestorType 是否更容易绑定得到Expander.DataContext。如果您想在 Brand 和 BoltPattern 之间添加更多间距,您还可以将 StackPanel 更改为类似 Grid 的东西:)
    • 如何在网格中添加间距之前实际上并没有使用网格
    • 如果您不熟悉 WPF 布局,我建议您阅读 WPF Layouts - A Quick Visual Start。它应该让您快速了解哪些布局控件可用,以及如何使用它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2011-02-10
    • 2016-12-05
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多