-
-
Save mingl0280/7e24ff52eb7af5ad8b127fee45288592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// MainWindow.xaml: | |
/// 注意Image的Source和TextBlock的Text属性 | |
<ListView x:Name="MaterialListView" Margin="565,28,0,24" SelectionMode="Single" AlternationCount="-1" ScrollViewer.CanContentScroll="True" ItemsSource="{Binding}" PreviewMouseDown="MaterialListView_PreviewMouseDown" PreviewMouseDoubleClick="MaterialListView_PreviewMouseDoubleClick"> | |
<ListView.ItemsPanel> | |
<ItemsPanelTemplate> | |
<WrapPanel/> | |
</ItemsPanelTemplate> | |
</ListView.ItemsPanel> | |
<ListView.ItemTemplate> | |
<DataTemplate> | |
<Grid Height="100" > | |
<Grid.RowDefinitions> | |
<RowDefinition Height="50"/> | |
<RowDefinition Height="25"/> | |
<RowDefinition Height="25"/> | |
</Grid.RowDefinitions> | |
<Image Source="{Binding MaterialUrl}"/> | |
<TextBlock Text="{Binding MaterialName}" Grid.Row="1" TextAlignment="Center"/> | |
<TextBlock Text="{Binding MaterialCount}" Grid.Row="2" TextAlignment="Center"/> | |
</Grid> | |
</DataTemplate> | |
</ListView.ItemTemplate> | |
<ListView.ItemContainerStyle> | |
<Style TargetType="{x:Type ListViewItem}"> | |
<EventSetter Event="MouseEnter" Handler="ListViewItem_MouseEnter"/> | |
<EventSetter Event="MouseLeave" Handler="ListViewItem_MouseLeave"/> | |
</Style> | |
</ListView.ItemContainerStyle> | |
</ListView> | |
/// Material.cs 这个是独立的一个类 | |
public class Material | |
{ | |
public string MaterialUrl { get; set; } | |
public string MaterialName { get; set; } | |
public string MaterialCount { get; set; } | |
public string MaterialID { get; set; } | |
public Material() | |
{ | |
MaterialUrl = ""; | |
MaterialName = ""; | |
MaterialCount = ""; | |
} | |
public Material(int id, string url, string name, int count) | |
{ | |
MaterialID = id.ToString(); | |
MaterialUrl = url; | |
MaterialName = name; | |
MaterialCount = "x" + count.ToString(); | |
if (count <= 0) | |
{ | |
MaterialCount = ""; | |
} | |
} | |
} | |
///添加物品代码(MainWindow.xaml.cs) | |
List<Material> mList = new List<Material>(); | |
///...数据提取, dt是dataTable | |
for (int i = 0; i < dt.Rows.Count; i++) | |
{ | |
if (rid != Convert.ToInt32(dt.Rows[i][0])) | |
{ | |
mList.Add(new Material(-1, "", "或", -1)); | |
rid = Convert.ToInt32(dt.Rows[i][0]); | |
} | |
mList.Add(new Material(Convert.ToInt32(dt.Rows[i][4]), dt.Rows[i][3].ToString(), dt.Rows[i][2].ToString(), Convert.ToInt32(dt.Rows[i][1]))); | |
} | |
MaterialListView.ItemsSource = mList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment