C#WPF ListView ItemsSource-设置文本和图片

科林·M

目前,在我的列表视图中,我正在使用以下代码设置文本,图片和文本颜色-http://oi61.tinypic.com/nzggls.jpg

在此处输入图片说明

foreach (Mods modname in gameMods)
{
    if (Directory.Exists(Path.Combine(ArmA3PATH, "@" + modname.ModString)))
    {
        lstMods.Items.Add(new listViewItem
                (
                    modname.ModName.ToString(),
                    Path.Combine(dir, modname.ModLink),
                    new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Green)
                )
            );
    }
    else
    {
        lstMods.Items.Add(new listViewItem
                (
                    modname.ModName.ToString(),
                    Path.Combine(dir, modname.ModLink),
                    new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red)
                )
          );
    }
}

这两个类如下所示,其中gameMods只是使用Mods创建的List,List

public class listViewItem
{
    public string Text { get; set; }
    public string ImagePic { get; set; }
    public System.Windows.Media.SolidColorBrush BackgroundColor { get; set; }
    public listViewItem(string text, string image, System.Windows.Media.SolidColorBrush color)
    {
        Text = text;
        ImagePic = image;
        BackgroundColor = color;
    }
}

public class Mods
{
    public string ModName { get; set; }
    public string ModVersion { get; set; }
    public string ModLink { get; set; }
    public string ModString { get; set; }
    public string ModLogo { get; set; }

    public Mods(string modName, string modVersion, string modLink, string modString, string modLogo)
    {
        this.ModName = modName;
        this.ModVersion = modVersion;
        this.ModLink = modLink;
        this.ModString = modString;
        this.ModLogo = modLogo;
    }
}

上面代码的XAML标记是

<ListView x:Name="lstMods">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <Image Source="{Binding ImagePic}" Width="80" Height="80" Stretch="Fill"/>
                <TextBlock Name="txtBlock" Text="{Binding Text}" Foreground="{Binding BackgroundColor}" VerticalAlignment="Center" TextAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
        <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), 
            RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
            ItemWidth="248"
            MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
            ItemHeight="{Binding (ListView.View).ItemHeight, 
            RelativeSource={RelativeSource AncestorType=ListView}}" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

现在,这很好用;但是我不能像这样使用SelectionChanged和SelectedItem来获取值,那么我将能够以什么方式在ListView上使用ItemsSource并仍将图像/文本添加到ListView块中?

当前,在执行lstMods.ItemsSource = gameMods时,ItemsSource看起来像这样http://oi59.tinypic.com/dwi0m.jpg

在此处输入图片说明

我知道这是因为没有绑定的文本值,但是我不太确定在何处添加这些值以用于“物料采购”。

科林·M

今天看了几分钟后,我所做的只是我最初寻找的以下内容

rivate void lstMods_DblClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    var selection = lstMods.SelectedItem;
    var mod = selection as listViewItem;

    MessageBox.Show(mod.LinkUrl);
}

public class listViewItem
{
    public string Text { get; set; }
    public string ImagePic { get; set; }
    public string LinkUrl { get; set; }
    public System.Windows.Media.SolidColorBrush BackgroundColor { get; set; }
    public listViewItem(string text, string image, System.Windows.Media.SolidColorBrush color, string modlink)
    {
        Text = text;
        ImagePic = image;
        BackgroundColor = color;
        LinkUrl = modlink;
    }
}

foreach (Mods modname in gameMods)
{
    if (Directory.Exists(Path.Combine(ArmA3PATH, "@" + modname.ModString)))
    {
        lstMods.Items.Add(new listViewItem
                (
                    modname.ModName.ToString(),
                    Path.Combine(dir, modname.ModLink),
                    new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Green),
                    modname.ModLink.ToString()
                )
            );
    }
    else
    {
        lstMods.Items.Add(new listViewItem
                (
                    modname.ModName.ToString(),
                    Path.Combine(dir, modname.ModLink),
                    new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red),
                    modname.ModLink.ToString()
                )
          );
    }
}

XAML被

<ListView x:Name="lstMods" MouseDoubleClick="lstMods_DblClick">

因此,实际上,我所做的唯一更改是,最初我将SelectionChanged称为“ Mods”类,而应该将其作为listViewItem类。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何设置ListView的ItemsSource?

来自分类Dev

XAML中的WPF ListView绑定ItemsSource

来自分类Dev

来自ItemsSource的数据-ListView中的文本包装

来自分类Dev

C#WPF DataGrid.ItemsSource生成新列

来自分类Dev

C#WPF扩展ListView行

来自分类Dev

如何从Universal(wpf)上的“ listview.ItemsSource”获取item [0]

来自分类Dev

使用ItemsSource和ItemTemplate的WPF ListBox

来自分类Dev

使用ItemsSource和ItemTemplate的WPF ListBox

来自分类Dev

使用多个Gridview C#WPF更新ListView

来自分类Dev

C#WPF将字典绑定到Listview

来自分类Dev

c#wpf:动态导入Listview项类型样式

来自分类Dev

C#WPF列在ListView中的对齐方式

来自分类Dev

C#WPF BorderBrush不会设置颜色

来自分类Dev

通过INotifyPropertyChanged更新ListView的ItemsSource

来自分类Dev

C#WPF捕获键盘和鼠标

来自分类Dev

C#WPF图像加载和保存

来自分类Dev

C#WPF OnMouseEnter和OnMouseLeave循环

来自分类Dev

ItemsSource更新后,WPF ComboBox设置为null

来自分类Dev

WPF使用数据/设置ItemsSource填充Datagrid的DataGridCheckBoxColumn

来自分类Dev

WPF ComboBox绑定ItemsSource

来自分类Dev

当在XAML中“绑定” ItemsSource时,ListView是空白的,但是如果直接设置属性,则会填充ListView?

来自分类Dev

获取WPF控件以触发和设置另一个控件的itemssource

来自分类Dev

如何从组合框C#WPF中获取文本?

来自分类Dev

C#WPF滑块值和文本框

来自分类Dev

C#WPF RichTextboxf检查是否有任何文本

来自分类Dev

C#WPF DataTemplate在属性上设置背景色

来自分类Dev

C#WPF将gif动画设置为背景

来自分类Dev

C#WPF为所有控件设置Tag = name

来自分类Dev

处理之前和之后的C#WPF更新标签-立即

Related 相关文章

热门标签

归档