【问题标题】:Editable ListView wpf可编辑的 ListView wpf
【发布时间】: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 新手。不知道如何动态使用数据网格

标签: c# .net wpf listview


【解决方案1】:

如果您希望能够在表格网格中编辑数据,您应该使用DataGrid 控件。它基本上是一个可编辑的GridView。

创建一个代表一行的类:

public class Item
{
    public string Value { get; set; }
}

并将DataGrid 的ItemsSource 属性设置为此类对象的集合:

string aa = "aa,xx,cc,vv,bbb,hh,gg,rr,tt,yy,uu,ooo";
_table.ItemsSource = aa.Split(',').Select(x => new Item() { Value = x });

XAML:

<DataGrid x:Name="_table" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Databases" Binding="{Binding Value}" />
    </DataGrid.Columns>
</DataGrid>

【讨论】:

    【解决方案2】:

    您正在第一个 ControlTemplate 内创建另一个 ListView,这就是造成混乱的原因:

      <ListView ItemsSource="{Binding EditableItems}" >
            <ListView.View>
                <GridView >
                    <GridViewColumn Header="Databases" Width="498">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox  Text="{Binding .}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    

    【讨论】:

      【解决方案3】:

      我认为您使用的数据类型是错误的 如果你希望你的列表可以有一个编辑功能,你应该在这种情况下使用 observableCollection

      【讨论】:

      • OC 可以添加或删除值
      猜你喜欢
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多