Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...