来自另一个ListBox的ListBox项

时间:2014-10-17 11:27:01

标签: c# listbox windows-phone

在我的Windows Phone应用程序中,我有两个ListBox。我需要在按下一个项目时(从ListBox1),该项填充ListBox2。 所以,我的LisBox1是从JSON(绑定)填充的。

下面的代码不起作用(错误:Value does not fall within the expected range.):

    public void addToList2(object sender, System.Windows.Input.GestureEventArgs e)
    {
        var dcs = ((FrameworkElement)sender).DataContext;
        Fields fi = (Fields)dcs;

        List2.Items.Add(fi);
    }

列表框:

<ListBox Name="List1" Hold="addToList2" ItemsSource="{Binding Items}" Margin="0,85,0,0" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="242" />
                                    <ColumnDefinition Width="128" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <StackPanel Margin="0,0,-62,17" Grid.ColumnSpan="3">
                                    <StackPanel.Background>
                                        <SolidColorBrush Color="#FF858585" Opacity="0.5"/>
                                    </StackPanel.Background>
                                    <TextBlock x:Name="NameTxt" Grid.Column="0" Text="{Binding descricao}" TextWrapping="Wrap" FontSize="20" Style="{StaticResource PhoneTextNormalStyle}"/>
                                    <TextBlock Grid.Column="1" Text="{Binding valor_preco_a, StringFormat=N2}" TextWrapping="Wrap" Margin="45,20,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                    <TextBlock Grid.Column="3" Text="{Binding codigo}" TextWrapping="Wrap" FontSize="35" Margin="370,-50,12,0" Style="{StaticResource PhoneTextNormalStyle}" Foreground="Blue"/>

                                </StackPanel>
                                <TextBlock Grid.Column="0" Text="R$" Margin="15,48,158,17" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        </Style>
                    </ListBox.ItemContainerStyle>
                </ListBox>

<ListBox Name="List2" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Margin="0,182,-66,0" Visibility="Collapsed">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17" Width="432" Height="100">
                                <StackPanel.Background>
                                    <SolidColorBrush Color="#FFE8FF00" Opacity="0.2"/>
                                </StackPanel.Background>
                                <TextBlock Grid.Column="0" Text="{Binding descricao}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>

                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

2 个答案:

答案 0 :(得分:1)

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<Listbox x:Name="listbox1" ItemsSource="{Binding listOneObjects}" 
          SelectedItem="{listOneSelectedItem}" >
       <i:Interaction.Triggers>
               <i:EventTrigger EventName="PressAndHold">
                   <i:InvokeCommandAction Command="{Binding TouchDownCommand}"/>
               </i:EventTrigger>
       </i:Interaction.Triggers>
</Listbox>

<Listbox x:Name="listbox2" ItemsSource="{Binding listTwoObjects, Mode=TwoWay}" 
         SelectedItem="{Binding listTwoSelectedItem, Mode=TwoWay}">    
</Listbox>

你的C#

using System.ComponentModel;

public class YourClassName : INotifyPropertyChanged
    private List<Object> _listOneObjects;
    public List<Object> listOneObjects 
    {
        get{ return _listOneObjects; }
        set{ _listOneObjects = value; OnPropertyChanged("listOneObjects"); }
    }

private Object _listOneSelectedItem;
public Object listOneSelectedItem
{
    get{ return _listOneSelectedItem; }
    set{ _listOneSelectedItem = value;  OnPropertyChanged("listOneSelectedItem");  }
}


private List<Object> _listTwoObjects ;
public List<Object> listTwoObjects 
{
    get{ return _listOneObjects; }
    set{ _listOneObjects = value; OnPropertyChanged("listTwoObjects "); }
}

private Object _listTwoSelectedItem
public Object listTwoSelectedItem
{
    get{ return _listTwoSelectedItem; }
    set{ _listTwoSelectedItem= value; OnPropertyChanged("listTwoSelectedItem"); }
}

public ICommand TouchDownCommand{ get{ return _TouchDownCommand; }

private _TouchDownCommand;

public YourClassName(){
   this._TouchDownCommand= new ActionCommand(TouchDownExecuted);
}

private void TouchDownExecuted(){
   listTwoObjects.clear();
   listTwoObjects.Add(listOneSelectedItem);
   OnPropertyChanged("listTwoObjects");
}

PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(String prop){
    PropertyChangedEventHandler handler = PropertyChanged;

    if(handler != null)
    {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }

}

这里有许多活动部件,因此您需要了解以下内容。

  1. 数据绑定
  2. 命令绑定
  3. 交互触发器
  4. 事件触发器
  5. 绑定模式

答案 1 :(得分:-1)

object myItem;
    public Form1()
    {
        InitializeComponent();
        listBox1.Items.Add("Paul");
        listBox1.Items.Add("George");
        listBox1.Items.Add("Nik");
    }

    private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {


        myItem = listBox1.SelectedItem;
        listBox2.Items.Add(myItem);
    }

就像你可以获得listbox1中的任何对象并在列表框2中显示它。只需用鼠标点击它

另一个答案是:

值得一读...

http://msdn.microsoft.com/en-us/library/ms171548.aspx

SendKeys.Send(&#34; R&#34)

这可能只会触发一次,您可能需要附加一个在MouseDown事件上启动并在MouseUp事件上停止的计时器。然后你可以把SendKeys放在Timer.Tick事件中。

private void Form1_Load(object sender, EventArgs e)
{
    this.timer1.Interval = 500;
}

private void button1_MouseUp(object sender, MouseEventArgs  e) 
{
    timer1.Stop();
    this.Text = "moose-Up";
}

private void button1_MouseDown(object sender, EventArgs e)
{
    timer1.Start();
    this.Text = "moose-Down";
    this.richTextBox1.Select();
}

private void timer1_Tick(object sender, EventArgs e)
{
    SendKeys.Send("r");
    Debug.Print("tickling");
}

选择您希望收到SendKeys值的控件...