WPF如何将新数据放在旧数据下的ListView中的某行中

WPF How to put new data in some row in ListView under old data

我在使用 Listview 时遇到了问题,我真的不知道该怎么办。我在 ListView 中创建了 GridView,现在我必须调整 listview 中的数据,

我要做的就是在现有行中写入新的数据信息

新数据必须在同一行,颜色必须不同,新数据必须在旧数据之下。旧数据不变,只有2列和4列数据交叉

我的Xaml:

 <ListView x:Name="lbPersonList" Margin="30,98.4,362,150" ScrollViewer.VerticalScrollBarVisibility="Visible" 
               AlternationCount="2" >
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumnHeader Content="Product">

                    </GridViewColumnHeader>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" FontSize="18px" FontFamily="Arial" 
                                       FontWeight="Normal" Width="670" TextAlignment="Left"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn >
                    <GridViewColumnHeader Content="Unit price, €"
                         HorizontalAlignment="Right">
                    </GridViewColumnHeader>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Age}" FontSize="18px"  FontFamily="Arial" 
                                       FontWeight="Normal"  Width="100" TextAlignment="Right"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn >
                    <GridViewColumnHeader Content="Quantity"
                         HorizontalAlignment="Center">
                    </GridViewColumnHeader>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Country}" FontSize="18px"  FontFamily="Arial" 
                                       FontWeight="Normal"  Width="130" TextAlignment="Center"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn >
                    <GridViewColumnHeader Content="Price, €"
                         HorizontalAlignment="Right">
                    </GridViewColumnHeader>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding adress}" FontSize="18px" FontFamily="Arial" 
                                       FontWeight="Bold"  Width="100" TextAlignment="Right"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

.cs代码:

 public class Person
{
    public string Name { get; set; }
    public double Age { get; set; }
    public string Country { get; set; }
    public double adress { get; set; }
   // public bool IsDiscount { get; set; }
   // public string discounText { get; set; }
   // public double discount { get; set; }
}

public partial class MainWindow : Window
{
    ObservableCollection<Person> myList;

    public MainWindow()
    {
        InitializeComponent();
        myList = new ObservableCollection<Person>()
        {
            new Person{ Name="Name 1", Age=234444, Country="India", adress=1.01},
            new Person{ Name="Name 2", Age=24, Country="India1", adress=12.45},
            new Person{ Name="Name 3", Age=25, Country="India2", adress=45.84},
            new Person{ Name="Name 4", Age=26, Country="India3", adress=12},
            new Person{ Name="Name 5", Age=27, Country="India4", adress=41.21},
            new Person{ Name="Name 6", Age=28, Country="India5", adress=15},
            new Person{ Name="Name 7", Age=29, Country="India6", adress=8},
            new Person{ Name="Name 8", Age=30, Country="India7", adress=9.11},
            new Person{ Name="Name 9", Age=31, Country="India8", adress=7.99},
            new Person{ Name="Name 10", Age=32, Country="India9", adress=18},
            new Person{ Name="Name 11", Age=33, Country="India10", adress=74},
            new Person{ Name="Name 12", Age=34, Country="India11", adress=78.21},
            new Person{ Name="Name 13", Age=35, Country="India12", adress=101.01},
            new Person{ Name="Name 14", Age=36, Country="India13", adress=7},
            new Person{ Name="Name 15", Age=37, Country="India14", adress=9},
            new Person{ Name="Name 16", Age=38, Country="India15", adress=12.15},
            new Person{ Name="Name 17", Age=39, Country="India16", adress=0.14},
            new Person{ Name="Name 18", Age=40, Country="India17", adress=0.99},
            new Person{ Name="Name 19", Age=41, Country="India18", adress=98},
            new Person{ Name="Name 20", Age=42, Country="India19", adress=47},
            new Person{ Name="Name 21", Age=43, Country="India21", adress=1.99},
            new Person{ Name="Name 22", Age=44, Country="India21", adress=1.90},
        };
        lbPersonList.ItemsSource = myList;


    }
}

============================================= ==

编辑:我得到了我想要的:

 <GridViewColumn>

                        <GridViewColumnHeader Content="Product">

                        </GridViewColumnHeader>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <Grid>
                                    <TextBlock Text="{Binding odd}"  FontSize="18px" FontFamily="Arial" 
                                       FontWeight="Normal" Width="670" TextAlignment="Left"/>
                                </Grid>
                                <TextBlock  Foreground="#B30C0C" FontFamily="Arial" FontWeight="Normal" FontSize="18px" Text="{Binding discounText}" Visibility="{Binding IsDiscount, Converter={StaticResource VisibilityConverter}}" />
                            </StackPanel>

                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn >



 List<MyData> sampleData = new List<MyData>
      {
         new MyData{odd="Name 1", unitPrice=455.45, quantity ="India1", Price=41.25, 
             IsDiscount=false, discounText="Surname 1", discountUnit=186, discountPrice=1.01},
         new MyData{odd="Name 2", unitPrice=41.87, quantity ="India2", Price=47.56,
             IsDiscount=false, discounText="Surname 2", discountUnit=84, discountPrice=12.45},
         new MyData{odd="Name 3", unitPrice=234, quantity ="India3", Price=1.01,
             IsDiscount=false, discounText="Surname 3", discountUnit=2744, discountPrice=45.84},
         new MyData{odd="Name 4", unitPrice=2.45, quantity ="India4", Price=1.04,
             IsDiscount=false, discounText="Surname 4", discountUnit=852, discountPrice=12},...

我也用public class VisibilityConverter : IValueConverter

我会说你只需要修改每一列的单元格模板。创建一个垂直的 StackPanel 并将原始文本作为第一行,将红色文本作为第二行。您是否显示第二行将与可见性标志相关联。就像您在第一行删除删除线一样。应该很简单。您只需要将属性添加到您的 Person class.

我不会为你写东西,但看起来很简单

<StackPanel Orientation="Vertical">
    <Grid>
       <TextBlock Text="{Binding Row1} VerticalAlignment="Center" />
       <Rectangle Height="1" Fill="Red" VerticalAlignment="Center" Visibility={Binding Row2Visibility} />
    </Grid>
    <TextBlock Foreground="Red" Text="{Binding Row2} Visibility={Binding Row2Visibility} />
</StackPanel>

这就像我要做的 5 秒模拟 :)。垂直堆栈面板包含第一行和第二行文本。第 2 行文本具有与其相关的可见性...第 1 行,我将其放在网格内,因此很容易划线。如果你想通过 "right" 方式进行删除,那将需要克隆系统字体并添加删除线......当真正的删除线只是一条线时,所以没什么大不了的。删除线的可见性显然与第二行文本的可见性相同。它们要么都可见,要么都折叠。