ListView中的自定义ListViewItem

Mur*_*sli 1 c# wpf user-controls listview

在ListViewItem WPF中显示项目的可能方法

更新: 在此处输入图片说明

那是我需要添加到ListView的控件,在这里我只需要显示计算机名,但该项目仍应包含计算机地址

在此处输入图片说明

稍后,我将需要具有表示文件和文件夹的项目的ListView,这些文件和文件夹将具有:名称,路径,大小,图标,IsFile属性。

所以这就是我现在要处理的,我卡在listView中,当我切换到WPF时我没想到会发生这种情况

Fed*_*gui 6

老兄,我在 5 分钟内用几行 XAML 制作了一个简单的“文件资源管理器”示例。

没有必要创建自己的ListViewItem或类似的东西。WPF 不是 winform。你需要明白这一点。

我已经向你解释了这一点。UI 不是数据

看:

<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>
Run Code Online (Sandbox Code Playgroud)

背后的代码:

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();

    }
}
Run Code Online (Sandbox Code Playgroud)

数据项:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明

你有看到?没有必要使任何事情变得过于复杂。WPF 拥有您做任何事情所需的一切。


Fed*_*gui 5

这是另一个示例:

<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>
Run Code Online (Sandbox Code Playgroud)

背后的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

数据项:

public class ComputerInfo
{
    public string Name { get; set; }
    public string Ip { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明