wpf listview按钮在行之间添加一行

时间:2014-08-29 09:04:33

标签: c# wpf xaml listview button

嘿stackoverflow communite

我的问题是我得到了一个带有不同绑定的列表视图(参见图1和XAML),当有人点击“+”按钮时,应在该点击的行上方插入一行,空白或在这种情况下使用00 00 00 00信息(见图2)。

照片 一个

http://www11.pic-upload.de/29.08.14/t4481qrginwm.png

图片二

http://www11.pic-upload.de/29.08.14/jdfys8nijkkb.png

XAML:                 

    <ListView Margin="49,61,0,0" HorizontalAlignment="Left" MaxWidth="600" VerticalAlignment="Top" Name="lvDataBinding" Height="227" Width="402" >

        <ListView.ItemTemplate>
            <DataTemplate>
                <WrapPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding fieldblock_0}" Background="{Binding background_fieldblock_0}"  />
                    <TextBlock Text=" ][ " />
                    <TextBlock Text="{Binding fieldblock_1}" Background="{Binding background_fieldblock_1}"  />
                    <TextBlock Text=" ][ " />
                    <TextBlock Text="{Binding fieldblock_2}" Background="{Binding background_fieldblock_2}"  />
                    <TextBlock Text=" ][ " />
                    <TextBlock Text="{Binding fieldblock_3}" Background="{Binding background_fieldblock_3}" />
                    <TextBlock Text=" ][ " />
                    <TextBlock Text="{Binding fieldblock_4}" Background="{Binding background_fieldblock_4}" />
                    <TextBlock Text=" ]" />

                    <Button Click = "addrowbutton" Tag="{Binding}" Height="18" >+  </Button>
                </WrapPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    </Grid>

MainWindow.xaml.cs:

  public MainWindow()
    {
        InitializeComponent();
        List<mainpart_compare> items5 = new List<mainpart_compare>();
        for (int i = 0; i < 5; i++)
        {
            items5.Add(new mainpart_compare()
                      {
                          fieldblock_0 = "00 06 ",
                          fieldblock_1 = "00 06 ",
                          fieldblock_2 = "00 06 ",
                          fieldblock_3 = "00 06 ",
                          fieldblock_4 = "00 06 ",
                          background_fieldblock_0 = "white",
                          background_fieldblock_1 = "white",
                          background_fieldblock_2 = "white",
                          background_fieldblock_3 = "white",
                          background_fieldblock_4 = "white",
                      });

        }
        lvDataBinding.ItemsSource = items5;
    }

    public class mainpart_compare
    {

        // Goto Field information
        public string fieldblock_0 { get; set; }
        public string fieldblock_1 { get; set; }
        public string fieldblock_2 { get; set; }
        public string fieldblock_3 { get; set; }
        public string fieldblock_4 { get; set; }
        public string background_fieldblock_0 { get; set; }
        public string background_fieldblock_1 { get; set; }
        public string background_fieldblock_2 { get; set; }
        public string background_fieldblock_3 { get; set; }
        public string background_fieldblock_4 { get; set; }
    }

    private void addrowbutton(object sender, RoutedEventArgs e)
    {
        // here is the part where i dont know what to do.

    }

2 个答案:

答案 0 :(得分:1)

我的想法是确定你按下的行数,然后在这个地方插入你的列表。你的items5是我的List,虽然我把它创建为ObservaebleCollection以便反映任何变化。

 <ListView ItemsSource="{Binding Lista}" AlternationCount="{Binding Lista.Count}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="40" Text="{Binding (ItemsControl.AlternationIndex),RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
                    <TextBlock Width="40" Text="{Binding }"/>
                    <Button Width="40" Content="+" Command="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.Cmd}"
                            CommandParameter="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    public ObservableCollection<int> Lista { get; set; }
    private RelayCommand cmd;
    public RelayCommand Cmd
    {
        get { return cmd ?? (cmd = new RelayCommand(new Action<object>(AddRow))); }
    }
    public MainWindow()
    {
        Lista = new ObservableCollection<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        InitializeComponent();
        this.DataContext = this;
    }

    void AddRow(object obj)
    {
        int numer;
        Int32.TryParse(obj.ToString(), out numer);
        Lista.Insert(numer, 55);           
    }

我一直插入55以查看更改。正常输出:
enter image description here
第一列是行号,第二列是List中的值。你将[00 ...]改为。当我点击最后一行时,我将添加55,因为

Lista.Insert(numer, 55);    


enter image description here

答案 1 :(得分:0)

谢谢答案。 我想稍微修改一下代码,但不知怎的,它甚至没有向我显示任何内容,而是显示错误:

  

BindingExpression路径错误:'对象'''MainWindow'(Name ='')'上找不到'Lista'属性。 BindingExpression:路径=利斯塔; DataItem ='MainWindow'(Name ='');

  <ListView ItemsSource="{Binding lista}" AlternationCount="{Binding Lista.Count}" Name="TestVIew">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="40" Text="{Binding (ItemsControl.AlternationIndex),RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
                    <TextBlock Width="40" Text="{Binding Text_nr1}"Background="{Binding Text_nr1_background}"/>
                    <TextBlock Width="40" Text="{Binding Text_nr2}" Background="{Binding Text_nr1_background}"/>

                    <Button Width="40" Content="+" Command="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.Cmd}"
                        CommandParameter="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

现在我想更改2个不同TextBlocks (Background="{Binding Text_nr1_background}")的背景。

    public ObservableCollection<Lista> lista;
    private RelayCommand cmd;
    public RelayCommand Cmd
    {
        get { return cmd ?? (cmd = new RelayCommand(new Action<object>(AddRow))); }
    }
    public MainWindow()
    {
        lista = new ObservableCollection<Lista>() { new Lista() { Text_nr1 = "test", Text_nr1_background="green", Text_nr2_background="red",Text_nr2 = "test2" } };
        InitializeComponent();
        this.DataContext = this;
    }

    void AddRow(object obj)
    {
        int numer;
        Int32.TryParse(obj.ToString(), out numer);
        lista.Insert(numer, new Lista() { Text_nr1 = "test", Text_nr1_background = "green", Text_nr2_background = "red", Text_nr2 = "test2" });
    }

Lista类:

public class Lista
{
    public string Text_nr1 { get; set; }
    public string Text_nr1_background { get; set; }
    public string Text_nr2 { get; set; }
    public string Text_nr2_background { get; set; }  
}