Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

c# - Custom ListViewItem in ListView

possible ways to show item in ListViewItem WPF

Update: enter image description here

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

enter image description here

later, i will need ListView with Items that represent Files and Folders which will have : Name, Path, Size, Icon, IsFile properties.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...