【发布时间】:2018-03-19 14:51:15
【问题描述】:
我正在尝试动态创建可编辑的列表视图。需要将字符串数组显示为源。当我们单击该项目时,它应该是可编辑的。
问题:没有错误但没有项目显示。
Style tableStyle = _resourceDictionary["TableStyle"] as Style;
ItemsPanelTemplate itemsPanelTemplate = _resourceDictionary["ItemsPanelTemplate"] as ItemsPanelTemplate;
FrameworkElementFactory factoryPanel = new FrameworkElementFactory(typeof(UniformGrid));
factoryPanel.SetValue(UniformGrid.ColumnsProperty, 5);
ItemsPanelTemplate template = new ItemsPanelTemplate();
template.VisualTree = factoryPanel;
ListView simpleTable = new ListView();
// simpleTable.Columns.Add(new DataGridTextColumn { Header = "Values" });
simpleTable.Style = tableStyle;
simpleTable.ItemsPanel = template;
_table = simpleTable;
string aa = "aa,xx,cc,vv,bbb,hh,gg,rr,tt,yy,uu,ooo";
_table.ItemsSource = aa.ToString().Split(',');
Xaml
<Style x:Key="TableStyle" TargetType="{x:Type ListView}">
<Setter Property="Background" Value="{StaticResource ControlBackgroundBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListView}">
<ListView>
<ListView.View>
<GridView >
<GridViewColumn Header="Databases" Width="498">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding .}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【问题讨论】:
-
为什么不使用 DataGrid?
-
我是 WPF 新手。不知道如何动态使用数据网格