none
ListBox - Evento ao adicionar um item RRS feed

Respostas

  • Olá!
    Você já tentou o evento CurrentChanged do ListBox.Items?
    Segue um exemplo:



     

    public Window4()
    {
    InitializeComponent();
    this.lstBox.Items.CurrentChanged += new EventHandler(Items_CurrentChanged);

    List
    <Person> personList = new List<Person>();
    personList.Add(
    new Person() { Name = "Anderson" });

     

    this.lstBox.ItemsSource = personList;

     

    }

     

    void Items_CurrentChanged(object sender, EventArgs e)
    {
    // aqui vc implementa o codigo q será executado quando alterar
    }

     

    Anderson

    • Marcado como Resposta Cleidson França quarta-feira, 11 de agosto de 2010 17:27
    terça-feira, 10 de agosto de 2010 22:01

Todas as Respostas

  • Cleidson,

    Veja se isto te ajuda:

    http://www.wpfmentor.com/2008/11/notification-when-items-are-added-or.html


    André Alves de Lima
    Visite o meu site: http://andrealveslima.spaces.live.com
    Me siga no Twitter: @andrealveslima
    sexta-feira, 6 de agosto de 2010 18:43
    Moderador
  • Valeu!!

    Grato!!!

    sexta-feira, 6 de agosto de 2010 18:48
  • Magina! :)
    André Alves de Lima
    Visite o meu site: http://andrealveslima.spaces.live.com
    Me siga no Twitter: @andrealveslima
    sexta-feira, 6 de agosto de 2010 18:54
    Moderador
  • Olá Andre,

     

    Não funcionou não cara...

    Alguma idéia?

    Estou usando um <ListBox.ItemTemplate>, alguma idéia de quando adicionar um item eu possa tratar uns dados?

     

    Atc,

     

    Cleidson França

    terça-feira, 10 de agosto de 2010 18:13
  • Cleidson,

    Posta como está o seu XAML e code behind aí pra gente...


    André Alves de Lima
    Visite o meu site: http://andrealveslima.spaces.live.com
    Me siga no Twitter: @andrealveslima
    terça-feira, 10 de agosto de 2010 20:00
    Moderador
  • Segue...
    
    xaml
    
    <Window
    	xmlns:FTM="clr-namespace:Futura.PopTrade.WPF"
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	x:Class="Futura.PopTrade.WPF.winHistoricoTrade"
    	x:Name="Window"
    	Title="Futura Trade Messenger" Loaded="Window_Loaded" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="616" Width="473">
    
      <Window.Resources>
        <Style x:Key="TitleBorder" TargetType="Border">
          <Setter Property="CornerRadius" Value="10"/>
          <Setter Property="Background" Value="#FFDEDEDE"/>
          <Setter Property="Margin" Value="0,0,5,0"/>
          <Setter Property="Grid.Column" Value="0"/>
        </Style>
    
        <Style x:Key="TitleText" TargetType="TextBlock">
          <Setter Property="FontSize" Value="16"/>
          <Setter Property="Foreground" Value="#FF14517B"/>
          <Setter Property="Margin" Value="10,3,0,0"/>
        </Style>
    
        <Style x:Key="TopGrid" TargetType="Grid">
          <Setter Property="Background" Value="#FF5C7590" />
        </Style>
    
        <Style x:Key="Header" TargetType="Grid">
          <Setter Property="Margin" Value="7" />
          <Setter Property="Grid.Row" Value="0"/>
        </Style>
    
        <Style x:Key="SearchBox" TargetType="TextBox">
          <Setter Property="Grid.Column" Value="1"/>
          <Setter Property="FontSize" Value="12"/>
        </Style>
    
        <Style x:Key="SearchButton" TargetType="Button">
          <Setter Property="Grid.Column" Value="2"/>
        </Style>
    
        <Style x:Key="StoriesList" TargetType="ListBox">
          <Setter Property="Margin" Value="5"/>
          <Setter Property="Grid.Row" Value="1"/>
        </Style>
    
        <Style x:Key="pnlHistorico" TargetType="StackPanel">
          <Setter Property="Margin" Value="10"/>
          <Setter Property="Width" Value="68"/>
          <Setter Property="Height" Value="68"/>
          <Setter Property="Background">
            <Setter.Value>
              <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFFFF098"/>
                <GradientStop Color="#FFFFF9D4" Offset="1"/>
              </LinearGradientBrush>
            </Setter.Value>
          </Setter>
        </Style>
    
        <Style x:Key="DetailCol1" TargetType="TextBlock">
          <Setter Property="HorizontalAlignment" Value="Center"/>
          <Setter Property="FontSize" Value="18"/>
          <Setter Property="FontWeight" Value="Bold"/>
          <Setter Property="Foreground" Value="DarkSlateGray"/>
        </Style>
    
        <Style x:Key="DetailCol2" TargetType="TextBlock">
          <Setter Property="HorizontalAlignment" Value="Center"/>
          <Setter Property="FontSize" Value="14"/>
          <Setter Property="Foreground" Value="DarkSlateGray"/>
        </Style>
    
        <Style x:Key="ImagePreview" TargetType="Image">
          <Setter Property="Margin" Value="7,7,5,5"/>
          <Setter Property="Height" Value="55"/>
        </Style>
    
        <Style x:Key="TitleBlock" TargetType="TextBlock">
          <Setter Property="FontSize" Value="12"/>
          <Setter Property="TextAlignment" Value="Left"/>
          <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
      </Window.Resources>
    
      <Grid Style="{StaticResource TopGrid}">
    
        <Grid.RowDefinitions>
          <RowDefinition Height="42"/>
          <RowDefinition Height="31*"/>
          <RowDefinition Height="504*" />
        </Grid.RowDefinitions>
    
        <Grid Style="{StaticResource Header}">
    
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition Width="50"/>
          </Grid.ColumnDefinitions>
    
          <Border Style="{StaticResource TitleBorder}">
            <TextBlock Text="Histórico de Trades"  Style="{StaticResource TitleText}" />
          </Border>
    
          <TextBox x:Name="txtAtivo" Text="Ativo" Padding="1,3,1,1" HorizontalAlignment="Left" Width="61" Grid.Column="1" Margin="-1,0,0,0"/>
          <Button x:Name="btnSearch" 
    	          Content="Buscar" 						          
    						RenderTransformOrigin="3.544,0.725"
    						 Grid.Column="1" 
    						 Margin="64,0,46,0" 
    						 Style="{StaticResource SearchButton}"/>
        </Grid>
        <ListBox x:Name="ListaHistoricoTrades" Style="{StaticResource StoriesList}" Margin="5,0,5,12" Grid.Row="2" DataContextChanged="ListaHistoricoTrades_DataContextChanged">
          <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Orientation="Horizontal" Height="82" Width="432">
              <Image Source="c:\dollar-icon.ico" Style="{StaticResource ImagePreview}"/>
              <!-- Yellow panel-->
              <StackPanel Style="{StaticResource pnlHistorico}">
                <TextBlock x:Name="papel" Text="{Binding Path=Ativo}" Style="{StaticResource DetailCol1}"/>
                <TextBlock x:Name="tipoCall" Text="" Style="{StaticResource DetailCol2}" />
              </StackPanel>
              <StackPanel>
                <Label Content="Entrada" />
                <Label Content="Objetivo" />
                <Label Content="Saída" />
              </StackPanel>
              <StackPanel>
                <TextBlock x:Name="entrada" Text="" Margin="5" Style="{StaticResource TitleBlock}" TextWrapping="Wrap" Width="27" />
                <TextBlock x:Name="objetivo" Text="" Margin="5" Style="{StaticResource TitleBlock}" TextWrapping="Wrap" Width="27" />
                <TextBlock x:Name="saida" Text="" Margin="5" Style="{StaticResource TitleBlock}" TextWrapping="Wrap" Width="27" />
              </StackPanel>
              <StackPanel Width="140">
                <TextBlock x:Name="comentarios" Text="{Binding Path=Comentario}" TextWrapping="Wrap" Height="74" Margin="6,5,8,0"/>
              </StackPanel>
              <StackPanel RenderTransformOrigin="1.898,0.587" Width="51">
                <Image Source="c:/mais.gif" Height="76" Width="32" Margin="2,0,11,0"	/>
              </StackPanel>
            </StackPanel>
          </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
        <CheckBox Content="Ações" HorizontalAlignment="Left" Margin="111,5,0,10.098" Grid.Row="1" d:LayoutOverrides="Height" Width="55.683"/>
        <CheckBox Content="Opções" Margin="184,4,210,11.098" Grid.Row="1"/>
        <CheckBox Content="Avisos" Margin="0,4,139,11.098" Grid.Row="1" HorizontalAlignment="Right" Width="56" RenderTransformOrigin="-0.179,0.752"/>
        <Label Content="Exibir:" HorizontalAlignment="Left" Margin="47,0,0,4" Grid.Row="1" Width="42.993"/>
      </Grid>
    </Window>
    
    <br/>
    
    <pre lang="x-c#">code behind 
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using Futura.WinForms.Commom;
    using System.Windows.Forms;
    using Futura.PopTrade.Core.VO;
    using System.Collections.Specialized;
    
    namespace Futura.PopTrade.WPF
    {
    	/// <summary>
    	/// Interaction logic for Window1.xaml
    	/// </summary>
    	public partial class winHistoricoTrade : Window
    	{
    		public winHistoricoTrade()
    		{
    			this.InitializeComponent();
         
          teste();
    			// Insert code required on object creation below this point.
    		}
        public void AjustaTamanhoForm()
        {
          int _heightTaskBar;
          bool _autoHideTaskBar;
          int _height;
          TaskBarInfo TaskBar = new TaskBarInfo();
          TaskBarInfo.TaskBarEdge TaskEdge = new TaskBarInfo.TaskBarEdge();
          TaskBar.GetTaskBarInfo(out TaskEdge, out _heightTaskBar, out _autoHideTaskBar);
    
          _height = Screen.PrimaryScreen.Bounds.Height;
          _height = _height - _heightTaskBar;
          this.Height = _height;
        }
      //DADOS PARA TESTE
        private void teste()
        {
          List<Futura.PopTrade.Core.VO.TradeAcao> ListaTrade = new List<Core.VO.TradeAcao>();
    
          TradeAcao trade = new TradeAcao();
    
          trade.ID = 1;
          trade.Ativo = "PETR4";
          trade.Comentario = "teste1";
          // Popular Vo
    
          ListaTrade.Add(trade);
    
          trade = new TradeAcao();
          trade.ID = 2;
          trade.Ativo = "VALE5";
          trade.Comentario = "teste2";
          // Popular Vo
    
          ListaTrade.Add(trade);
          ListaHistoricoTrades.ItemsSource = ListaTrade;
        }
    
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
          AjustaTamanhoForm();
        }
        
    	}
    }
    

    terça-feira, 10 de agosto de 2010 20:10
  • Olá!
    Você já tentou o evento CurrentChanged do ListBox.Items?
    Segue um exemplo:



     

    public Window4()
    {
    InitializeComponent();
    this.lstBox.Items.CurrentChanged += new EventHandler(Items_CurrentChanged);

    List
    <Person> personList = new List<Person>();
    personList.Add(
    new Person() { Name = "Anderson" });

     

    this.lstBox.ItemsSource = personList;

     

    }

     

    void Items_CurrentChanged(object sender, EventArgs e)
    {
    // aqui vc implementa o codigo q será executado quando alterar
    }

     

    Anderson

    • Marcado como Resposta Cleidson França quarta-feira, 11 de agosto de 2010 17:27
    terça-feira, 10 de agosto de 2010 22:01
  • Funcionou. valeu!
    quarta-feira, 11 de agosto de 2010 17:27