目前,在我的列表视图中,我正在使用以下代码设置文本,图片和文本颜色-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
我知道这是因为没有绑定的文本值,但是我不太确定在何处添加这些值以用于“物料采购”。
今天看了几分钟后,我所做的只是我最初寻找的以下内容
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] 删除。
我来说两句