WPF ListView collection bindings

I have implemented a set 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 populated successfully, now I am associating a ListIssueLinks view with this collection.

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

      

Here I have a problem, the problem is that I am new to WPF and have no idea how to properly implement the formatting (for example, to present a NavigateUri or Name in the UI only) and implement a generic handler for clicking 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've tried DataTemplate, tried many other options, searched a lot, but still don't know how to do it. Could you suggest any light and elegant solution?

+3


source to share


2 answers


DataTemplate

- your best choice, this is 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 will just link directly to yours issueLinks

(no need to create HyperLink

in code).



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

ListIssueLinks.ItemsSource = issueLinks;

      

Then your event handler RequestNavigate

should 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;
}

      

+1


source


I haven't fully understood what the problem is, but I have two things you should know about existence.

  • ObservableCollection I think you should use it instead of a simple list because it has advantages if you need to link List to The View to your ListView for example. read more here: ObservableCollection class

  • XAML side the second thing I think you were trying to explain is how to take properties and show them in your list. this means you need to write it down on the XAML side and study it further, you can bind the properties after that with inotifypropertychanged if the data in the list changes for any reason.



hope i helped.

0


source







All Articles