目前在我的列表視圖我設置文字,圖片下面的代碼看起來像這樣&文本顏色 - http://oi61.tinypic.com/nzggls.jpgC#WPF ListView控件的ItemsSource - 設置文本和圖片
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創建的列表,列表
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標記fo r上面的代碼是
<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>
現在,這工作得很好;但我不能像這樣使用SelectionClass來獲取值,那麼我怎樣才能在ListView上使用ItemsSource並將圖像/文本添加到ListView塊?
目前,做lstMods.ItemsSource = gameMods當ItemsSource時,看起來像這樣http://oi59.tinypic.com/dwi0m.jpg
我知道那是因爲沒有綁定文本值,但我不能肯定在哪裏添加Item Sourcing的這些值。
你正在使用WPF的人!只要將SelectedItem綁定爲屬性,只要實現INotifyPropertyChanged,您就可以訂閱更改。如果你創建了一個ObservableCollection(就像你在.Net中聲明一個List一樣),你可以添加和刪除Mod,並且這些改變將在ListView上直觀地反映出來。爲當前選定的一個創建一個Mod屬性。 –
2015-02-10 00:28:49
這不是我想要引用的更改,它們都是通過XML文件處理的,無論如何都是爲了服務器管理員更新或修改;它更通過ItemsSource填充列表以將gameList信息附加到該ListView項目,以便您可以檢索有關所選項目的信息。 去看看一些MSDN文章,看看我能用它爲SelectedItem或ItemsSource&SelectionChanged – 2015-02-10 10:48:21