First off, I am very new to WPF. These are the objects I want to display:
public class IssueWithComments
{
public Issue Issue { get; set; }
public IReadOnlyList<IssueComment> Comments { get; set; }
public IssueWithComments()
{
Comments = new List<IssueComment>();
}
}
// Simplified
public class Issue
{
public string Body { get; protected set; }
public Uri HtmlUrl { get; protected set; }
public IReadOnlyList<Label> Labels { get; protected set; }
public Milestone Milestone { get; protected set; }
public int Number { get; protected set; }
public PullRequest PullRequest { get; protected set; }
public ItemState State { get; protected set; }
public string Title { get; protected set; }}
public Uri Url { get; protected set; }
public User User { get; protected set; }
}
public class IssueComment
{
public string Body { get; protected set; }
public Uri HtmlUrl { get; protected set; }
public Uri Url { get; protected set; }
public User User { get; protected set; }
}
The following is my XAML:
Within MainWindow.xaml
<ScrollViewer DockPanel.Dock="Right" CanContentScroll="True" >
<StackPanel x:Name="grid" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
</ScrollViewer>
Within GithubIssue.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:IssuesManagment.UI.POC.Utils"
xmlns:Octokit="clr-namespace:Octokit;assembly=Octokit" x:Class="IssuesManagment.UI.POC.Controls.GithubIssue"
xmlns:models="clr-namespace:IssuesManagment;assembly=IssuesManagment.Models"
mc:Ignorable="d"
d:DesignHeight="147" d:DesignWidth="295" BorderBrush="#FFD84B4B" BorderThickness="1" Width="Auto" Height="Auto" Margin="0,0,0,2">
<UserControl.Resources>
<local:StringToBrushConverter x:Key="stringToBrushConverter"/>
<local:IntegerToVisiblityConverter x:Key="intToVisiblityConverter"/>
</UserControl.Resources>
<UserControl.DataContext>
<models:IssueWithComments/>
</UserControl.DataContext>
<StackPanel>
<TextBlock>
<Hyperlink NavigateUri="{Binding Issue.HtmlUrl}" RequestNavigate="Hyperlink_RequestNavigate">
<TextBlock x:Name="issueTitle" TextWrapping="Wrap" Text="{Binding Issue.Title}" FontWeight="Bold" FontSize="16" />
</Hyperlink>
</TextBlock>
<ItemsControl ItemsSource="{Binding Issue.Labels}" HorizontalAlignment="Left">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate >
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="Octokit:Label">
<TextBlock Text="{Binding Name}" Background="{Binding Color, Converter={StaticResource stringToBrushConverter}}" Foreground="White" Padding="2" MaxWidth="100" Margin="0,0,1,0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock x:Name="issueBody" TextWrapping="Wrap" Text="{Binding Issue.Body}" Margin="2"/>
<Expander Visibility="{Binding Comments.Count, Converter={StaticResource intToVisiblityConverter}}" Header="Comments">
<ItemsControl Name="comments" ItemsSource="{Binding Comments}">
</ItemsControl>
</Expander>
</StackPanel>
</UserControl>
GithubIssueComment.xaml
<UserControl x:Class="IssuesManagment.UI.POC.Controls.GithubIssueComment" xmlns:Octokit="clr-namespace:Octokit;assembly=Octokit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignWidth="300" Width="Auto" Height="Auto">
<UserControl.DataContext>
<Octokit:IssueComment/>
</UserControl.DataContext>
<StackPanel>
<TextBlock Text="{Binding Body}" Padding="2" />
<Label Content="By:"/>
<TextBlock Text="{Binding User.Login}"/>
</StackPanel>
</UserControl>
C# Code:
Main.xaml.cs:
private async void ViewIssues_Click(object sender, RoutedEventArgs e)
{
grid.Children.Clear();
var apiClient = new Octokit.GitHubClient(new Octokit.ProductHeaderValue("Issue-Managment"));
if (!string.IsNullOrWhiteSpace(userName.Text) && !string.IsNullOrWhiteSpace(repoName.Text))
{
var issuesClient = new IssuesWithCommentsClient(new Octokit.ApiConnection(apiClient.Connection));
var issues = await issuesClient.GetAllForRepositoryWithComments(userName.Text, repoName.Text);
GithubIssue x;
foreach (var item in issues)
{
x = new GithubIssue(item);
grid.Children.Add(x);
}
}
else
{
grid.Children.Add(new Controls.Error());
}
}
GithubIssue.xaml.cs:
public GithubIssue(IssueWithComments issue)
{
InitializeComponent();
this.DataContext = issue;
var commentDisplayers = new List<GithubIssueComment>();
foreach (var comment in issue.Comments)
{
commentDisplayers.Add(new GithubIssueComment(comment));
}
comments.ItemsSource = commentDisplayers;
}
Where have I gone very off track, and where does it just need a little improvement?