2015-02-09 23 views
0

目前在我的列表視圖我設置文字,圖片下面的代碼看起來像這樣&文本顏色 - http://oi61.tinypic.com/nzggls.jpgC#WPF ListView控件的ItemsSource - 設置文本和圖片

enter image description here

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

enter image description here

我知道那是因爲沒有綁定文本值,但我不能肯定在哪裏添加Item Sourcing的這些值。

+0

你正在使用WPF的人!只要將SelectedItem綁定爲屬性,只要實現INotifyPropertyChanged,您就可以訂閱更改。如果你創建了一個ObservableCollection (就像你在.Net中聲明一個List一樣),你可以添加和刪除Mod,並且這些改變將在ListView上直觀地反映出來。爲當前選定的一個創建一個Mod屬性。 – 2015-02-10 00:28:49

+0

這不是我想要引用的更改,它們都是通過XML文件處理的,無論如何都是爲了服務器管理員更新或修改;它更通過ItemsSource填充列表以將gameList信息附加到該ListView項目,以便您可以檢索有關所選項目的信息。 去看看一些MSDN文章,看看我能用它爲SelectedItem或ItemsSource&SelectionChanged – 2015-02-10 10:48:21

回答

0

看待這個今天所有我做過的幾分鐘後,它做了以下我一直在尋找最初

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類時。

+0

加上,我知道我在這裏使用錯誤的選擇類改變了,這已經改變了我的代碼到MouseDoubleClick – 2015-02-10 16:39:46

0

正如@DanielLane所提到的,在WPF中,我們傾向於將數據綁定到UI中的ItemsControls。在你的情況,你應該包含您的listViewItem實例的Items(或類似的名稱)屬性:

<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屬性的setter方法將被調用:

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頁面以獲得進一步的幫助。