近来,因为工作原因,要做wpf。学习并用wpf写了几天,发现wpf真的很灵活。
以WPF 实现类似C#中ListView大图标模式来说,虽然有人用滚动视图加WrapPanel的自定义方式实现,但是我觉得没必要把问题想的太复杂,因为wpf本身是很灵活的。
下面是我从msdn的论坛上copy过来的代码,看看就知道了。
<ListBox Name="AppListBox" Background ="Transparent" ItemsSource="{Binding}" ScrollViewer.CanContentScroll="True">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" ></RowDefinition>
<RowDefinition Height="Auto" ></RowDefinition>
</Grid.RowDefinitions>
<Image Source="{Binding Pic}"/>
<TextBlock Text="{Binding Name}" Grid.Row="1"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
后台代码:
namespace StyleWorkAround
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<LVData> LVDatas = new ObservableCollection<LVData>();
public MainWindow()
{
InitializeComponent();
LVDatas.Add(new LVData { Name = "图ª?片?1", Pic = "http://www.google.com/intl/en_ALL/images/logo.gif" });
LVDatas.Add(new LVData { Name = "图ª?片?2", Pic = "http://www.google.com/intl/en_ALL/images/logo.gif" });
AppListBox.ItemsSource = LVDatas;
}
}
public class LVData
{
public string Name { get; set; }
public string Pic { get; set; }
}
}
这个代码是完全可以工作的。
来自:http://social.msdn.microsoft.com/Forums/zh-CN/wpfzhchs/thread/f9b5fb48-4373-4373-9609-01273dcd217f,感谢Sheldon _Xiao的为我们提供的代码。
其实,我想要的是下面的效果:
<ListBox.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64" ></ColumnDefinition>
<ColumnDefinition Width="64" ></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="{Binding Pic}"/>
<TextBlock Text="{Binding Name}" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
当然更改起来很简单了。
当然,你还有可以实现文件和链接的组合:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding xxx}" />
<Label x:Name="lblFormalList" HorizontalAlignment="Left" VerticalAlignment="Top" >
<Hyperlink x:Name="hyperlink" Click="hyperlink_Click" >
<TextBlock Text="嘻嘻嘻" />
</Hyperlink>
</Label>
</StackPanel>
</DataTemplate>
虽然上面用的都是listbox,我将其改为listview一样使用。