Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListView binding to the collection

I implemented a collection of Hyperlink elements using WPF:

var controlLinks = new List<Hyperlink>();

if (issueLinks != null)
{
    foreach (var link in issueLinks)
    {
        var tempLink = new Hyperlink()
        {
            NavigateUri = new Uri(link.link)
        };
        controlLinks.Add(tempLink);
    }
}
ListIssueLinks.ItemsSource = controlLinks;

Collections is successfuly filled, now I link ListIssueLinks view to this collection.

<ListView Name="ListIssueLinks" Height="100" >
    <ListView.View>
        <GridView>
            <GridViewColumn/>
        </GridView>
    </ListView.View>
</ListView>

Here I've got a problem, the issue is I'm new to WPF and have no idea how properly implement formatting (for example, to present NavigateUri or Name only on UI) and implement generic handler for click on any element. Something like this:

private void Hyperlink_OnClick(object sender, RoutedEventArgs e)
{
    var clickedLink = (Hyperlink) sender;
    System.Diagnostics.Process.Start(clickedLink.NavigateUri.ToString());
}

I tried DataTemplate, tried a lot of other variants, googled really a lot, but still have no clue how to do that. Could you please suggest any easy and elegant solution?

like image 392
Игорь Avatar asked Nov 19 '25 06:11

Игорь


2 Answers

DataTemplate is your best bet, here's how you could implement it in your case.

<ListView Name="ListIssueLinks" Height="100">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock>
            <Hyperlink NavigateUri="{Binding}" RequestNavigate="Link_RequestNavigate">
                <TextBlock Text="{Binding}" />
            </Hyperlink>
        </TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Then in your code behind, I would simply bind directly to your issueLinks (no need to build up HyperLinks in code).

List<string> issueLinks = new List<string>()
{
    "http://www.google.com",
    "http://www.stackoverflow.com",
};

ListIssueLinks.ItemsSource = issueLinks;

Then your RequestNavigate event handler needs to start your process

private void Link_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(e.Uri.AbsoluteUri));
    e.Handled = true;
}
like image 180
Niels Filter Avatar answered Nov 21 '25 18:11

Niels Filter


I didnt fully understand what is your problem but i have two things that you should know that exist.

  1. ObservableCollection i think you should consider use it instead of a simple List because it has advantages if you need to bind the List to The View with your ListView for example. for more info read here: ObservableCollection Class

  2. XAML side the second thing i think you tried to explain is how to take properties and show them in your list view. this means you need to write it on the XAML side and explore about it farther, you can bind the properties after that with inotifypropertychanged if the data in the list is going to change with any reason.

hope i helped.

like image 24
LamaTo Avatar answered Nov 21 '25 18:11

LamaTo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!