wpf ListView 中的自定义 ListViewItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20530010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom ListViewItem in ListView
提问by Murhaf Sousli
possible ways to show item in ListViewItem WPF
在 ListViewItem WPF 中显示项目的可能方法
Update:
更新:
that's the control i need to add to the ListView, in here i only need to display the Computer Name, still the item should hold the Computer Address
这是我需要添加到 ListView 的控件,在这里我只需要显示计算机名称,该项目仍然应该包含计算机地址


later, i will need ListView with Items that represent Files and Folders which will have : Name, Path, Size, Icon, IsFile properties.
稍后,我将需要带有代表文件和文件夹的项目的 ListView,这些项目将具有:名称、路径、大小、图标、IsFile 属性。
so this's what I'm dealing with right now, im stuck in listView which i didn't expect to happen when i switched to WPF
所以这就是我现在正在处理的问题,我被困在 listView 中,当我切换到 WPF 时我没想到会发生这种情况
回答by Federico Berasategui
Look dude, I made a simple "File Explorer" example in a couple of lines of XAML in 5 minutes.
老兄,我在 5 分钟内用几行 XAML 制作了一个简单的“文件资源管理器”示例。
There is NO need to create your own ListViewItemor anything like that. WPF is NOT winforms. You need to understand this.
没有必要创建自己的ListViewItem或类似的东西。WPF 不是 winform。你需要明白这一点。
I already explainedthis to you. UI is Not Data.
Look:
看:
<Window x:Class="WpfApplication14.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication14"
Title="MainWindow" Height="350" Width="525">
<ListView ItemsSource="{Binding}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Size">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Size" Text="{Binding Size}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsFile}" Value="False">
<Setter TargetName="Size" Property="Text" Value="[Directory]"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Path" DisplayMemberBinding="{Binding Path}"/>
</GridView>
</ListView.View>
</ListView>
</Window>
Code Behind:
背后的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var path = @"C:\";
var dirs = Directory.GetDirectories(path)
.Select(x => new DirectoryInfo(x))
.Select(x => new FileViewModel()
{
Name = x.Name,
Path = x.FullName,
IsFile = false,
});
var files = Directory.GetFiles(path)
.Select(x => new FileInfo(x))
.Select(x => new FileViewModel()
{
Name = x.Name,
Path = x.FullName,
Size = x.Length,
IsFile = true,
});
DataContext = dirs.Concat(files).ToList();
}
}
Data Item:
数据项:
public class FileViewModel: INotifyPropertyChanged
{
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
OnPropertyChanged("IsSelected");
}
}
public string Name { get; set; }
public long Size { get; set; }
public string Path { get; set; }
public bool IsFile { get; set; }
public ImageSource Image { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Result:
结果:


Do you see? There is no need to overcomplicate anything. WPF has everything you need to do anything.
你有看到?没有必要使任何事情变得过于复杂。WPF 拥有您做任何事情所需的一切。
回答by Federico Berasategui
Here is another example:
这是另一个例子:
<Window x:Class="WpfApplication14.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel>
<Button Content="Show Selected Computer" Click="Button_Click" DockPanel.Dock="Top"/>
<ListBox ItemsSource="{Binding}"
SelectedItem="{Binding SelectedComputer, RelativeSource={RelativeSource AncestorType=Window}}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel Margin="2">
<Rectangle Fill="Gray" Width="32" Height="32" DockPanel.Dock="Left"/>
<TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</Window>
Code Behind:
背后的代码:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = Enumerable.Range(1,10)
.Select(x => new ComputerInfo()
{
Name = "Computer" + x.ToString(),
Ip = "192.168.1." + x.ToString()
});
}
public ComputerInfo SelectedComputer { get; set; }
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(SelectedComputer.Ip);
}
}
Data Item:
数据项:
public class ComputerInfo
{
public string Name { get; set; }
public string Ip { get; set; }
}
Result:
结果:



