【发布时间】:2017-11-13 21:46:33
【问题描述】:
我正在尝试制作一个简单的 VS 2017 扩展,它正在获取一个对象并显示它。我有数据返回并在文本框中显示 json,所以我知道数据正确返回。但由于某种原因,gv 只显示了两次“id”这个词,因为它们是数据集中的两条记录。我已经尝试了很多事情,我正在迷失方向。另外,文档似乎到处都是。
我相信这里至少有两个问题...
1) XAML 的“绑定”
2) 将数据绑定或添加到 LV?
对此的任何帮助将不胜感激!
XAML
<UserControl x:Class="DoWork.AllWorkVSControl"
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"
Background="{DynamicResource VsBrush.Window}"
Foreground="{DynamicResource VsBrush.WindowText}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Name="MyToolWindow">
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Margin="10" HorizontalAlignment="Center">AllWorkVS</TextBlock>
<Button Content="Click me!" Click="button1_Click" Width="120" Height="80" Name="button1"/>
<TextBox Height="200" TextWrapping="Wrap" Name="txtJson"/>
<ListView x:Name="LvIssues">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="Auto" Header="Id" DisplayMemberBinding="{Binding Source='Id'}"></GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Grid>
</UserControl>
C#
public class People
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public partial class AllWorkVSControl : UserControl
{
public AllWorkVSControl()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var t = Issues.GetPeopleById("2");
PopulateListView();
MessageBox.Show(t);
}
private void PopulateListView()
{
var l = GetPeople();
txtJson.Text = JsonConvert.SerializeObject(l);
foreach (var p in l)
{
LvIssues.Items.Add(p);
}
}
}
【问题讨论】:
-
感谢您删减代码,但您删减了我们需要查看的内容。需要查看您在哪里设置窗口的 DataContext,您将其设置为什么,以及从窗口到您的列表视图的可视化树。
-
@Will 我更新了 xaml 部分。我没有window.datacontext。也许那是我的问题。我现在正试图找到“视觉树”。
-
PopulateListView-这个函数在哪里?
-
@AyyappanSubramanian 在 DoWork.xaml.cs 中。它是从单击窗口中触发的。我已经尝试更新以上内容。
-
可视化树是您在 XAML 中编写的内容。在运行时,它被编译和实例化,结果称为逻辑树,因为它通常与 XAML 非常不同。此外,Snoop 是在运行时检查绑定的好工具。
标签: c# wpf xaml gridview visual-studio-extensions