【问题标题】:C# WPF ListView ItemsSource - Setting Text & PictureC# WPF ListView ItemsSource - 设置文本和图片
【发布时间】:2015-02-09 22:50:07
【问题描述】:

目前在我的列表视图中,我正在使用如下代码设置文本、图片和文本颜色 - 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

我知道这是因为没有绑定文本值,但我不太确定在哪里为 Item Sourcing 添加这些值。

【问题讨论】:

  • 你正在使用 WPF 人!只需将 SelectedItem 作为属性绑定到,只要您实现 INotifyPropertyChanged,您就可以订阅更改。如果您创建 ObservableCollection(与在 .Net 中声明 List 的方式相同),您可以添加和删除 mods,并且更改将直观地反映在 ListView 上。为您当前选择的一个 Mod 属性创建一个。
  • 我想引用的不是更改,它们都是通过 XML 文件处理的,供服务器管理员更新或修改;它更多的是通过 ItemsSource 填充列表以将 gameList 信息附加到该 ListView 项目,以便您可以检索有关您选择的项目的信息。去看看一些 MSDN 文章,看看我可以用它为 SelectedItem 或 ItemsSource & SelectionChanged 做什么

标签: c# wpf listview visual-studio-2013


【解决方案1】:

正如@DanielLane 提到的,在 WPF 中,我们倾向于将集合数据绑定到 UI 中的ItemsControls。在您的情况下,您应该有一个 Items (或类似名称)属性来保存您的 listViewItem 实例:

<ListView x:Name="lstMods" ItemsSource="{Binding Items}">
    ...
</ListView>

然后您将代码更改为类似于以下内容:

Items.Add(new listViewItem
    (
        modname.ModName.ToString(),
        Path.Combine(dir, modname.ModLink),
        new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Green)
    )
);

要使其正常工作,您需要在具有Items 属性的类中实现INotifyPropertyChanged 接口。通常,我们会添加另一个 listViewItem 类型的属性,它表示集合中的选定项:

<ListView ItemsSource="{Binding Items}" SelectedItem="{Binding YourSelectedItem}">
    ...
</ListView>

现在,只要在 GridView 中选择了一个新项目,YourSelectedItem 属性设置器就会被调用:

private listViewItem yourSelectedItem;
public listViewItem YourSelectedItem
{ 
    get { return yourSelectedItem; }
    set
    {
        yourSelectedItem = value;
        NotifyPropertyChanged("YourSelectedItem");
        // The selected item changed so you can do something with the new item here
     }
}

请参阅 MSDN 上的Data Binding Overview 页面以获取更多帮助。

【讨论】:

    【解决方案2】:

    今天看了几分钟后,我所做的就是按照我最初的要求完成以下操作

    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 类。

    【讨论】:

    • 另外,我知道我在这里使用了错误的 Selection Changed 类,现在我的代码中已将其更改为 MouseDoubleClick
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多