C# 填充窗口的列
我要做的是在一个窗口中填充两列。此列将包含全局变量的名称及其路径。在窗口中显示所需内容时遇到问题C# 填充窗口的列,c#,wpf,data-binding,treeviewitem,treelistview,C#,Wpf,Data Binding,Treeviewitem,Treelistview,我要做的是在一个窗口中填充两列。此列将包含全局变量的名称及其路径。在窗口中显示所需内容时遇到问题 <TabItem Header="Global Variables" GotFocus="GlobalVariablesTab_GotFocus"> <dc:TreeListView Name="tvwGlobalVariables" dc:ColumnLayoutManager.Enabled="True" >
<TabItem Header="Global Variables" GotFocus="GlobalVariablesTab_GotFocus">
<dc:TreeListView Name="tvwGlobalVariables" dc:ColumnLayoutManager.Enabled="True" >
<dc:TreeListView.Columns>
<!--First Column. -->
<dc:StdGridViewColumn Header="Variable" Width="200" >
<dc:StdGridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextTrimming="CharacterEllipsis"/>
</DataTemplate>
</dc:StdGridViewColumn.CellTemplate>
</dc:StdGridViewColumn>
<!-- Second Column-->
<dc:StdGridViewColumn Header="Result" dc:ColumnRange.IsFillColumn="True"/>
</dc:TreeListView.Columns>
</dc:TreeListView>
</TabItem>
目前,我有lvi持有这两个部分,但我需要一个部分去第一列,另一个部分去另一列。如果我不需要lvi,我不打算保留它。无论哪种方式,我都需要在第一列中显示
entry.key
,在第二列中显示entry.value
。有什么想法吗?我不知道SchemaManager是什么,所以我只是使用了一个包含一些伪数据的哈希表来获得一个包含一些字典条目的枚举数。下面的示例代码使用一个常规WPF listview,它有两列,没有任何格式,以保持简单
XAML:
代码隐藏:
using System.Collections;
using System.Collections.Generic;
using System.Windows;
namespace TwoColsDemo
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Hashtable sman = new Hashtable();
sman.Add("Key1", "Value1");
sman.Add("Key2", "Value2");
Data = new List<DictionaryEntry>();
IEnumerator enumerator = sman.GetEnumerator();
while (enumerator.MoveNext())
{
DictionaryEntry entry = (DictionaryEntry)enumerator.Current;
Data.Add(entry);
}
DataContext = this;
}
public List<DictionaryEntry> Data { get; private set; }
}
}
使用系统集合;
使用System.Collections.Generic;
使用System.Windows;
名称空间TwoColsDemo
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
Hashtable sman=新的Hashtable();
添加(“键1”、“值1”);
添加(“键2”、“值2”);
数据=新列表();
IEnumerator枚举器=sman.GetEnumerator();
while(枚举数.MoveNext())
{
DictionaryEntry条目=(DictionaryEntry)枚举器.Current;
数据。添加(条目);
}
DataContext=this;
}
公共列表数据{get;private set;}
}
}
如果有人需要更多信息,请告诉我,我添加了我认为相关的内容,但我对所有这些都不太了解,所以这只是一个假设。我可以给你一些代码,在常规列表视图中显示两列。很难判断代码有什么问题,因为您使用的是一个专有的TreeListView,它甚至可能不起作用。但是仅仅从代码中猜测一下,我就发现了两个潜在的问题。首先,如果您对CellTemplates使用datatemplates,您希望使用数据绑定,而不是与ListViewItems混淆。在ListViewItems上设置标记不会导致显示任何内容。我只能显示这两列。我想对这些列进行写入。您能解释一下如何做到这一点吗?
<Window x:Class="TwoColsDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<ListView ItemsSource="{Binding Path=Data}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Key" DisplayMemberBinding="{Binding Path=Key}" />
<GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=Value}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Window>
using System.Collections;
using System.Collections.Generic;
using System.Windows;
namespace TwoColsDemo
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Hashtable sman = new Hashtable();
sman.Add("Key1", "Value1");
sman.Add("Key2", "Value2");
Data = new List<DictionaryEntry>();
IEnumerator enumerator = sman.GetEnumerator();
while (enumerator.MoveNext())
{
DictionaryEntry entry = (DictionaryEntry)enumerator.Current;
Data.Add(entry);
}
DataContext = this;
}
public List<DictionaryEntry> Data { get; private set; }
}
}