No binding SelectedItem listbox



  • I have a ListBox with e-mails, and I want a new window open on this ListBox element with the text of a selected message, so that a new window can open with a double application, but how do I download the text I need? I can't get SelectedIndex, which is when I press the SelectedIndex element, I'm not. Since the newcomers on this issue are very much asking for assistance in solving the problem. View

    <ListBox x:Name="listBox"
             ItemsSource="{Binding pop, Mode=TwoWay}"
             SelectedIndex="{Binding Path= SelectedIndex, Mode=TwoWay}">
    
    &lt;ListBox.ItemTemplate&gt;
        &lt;DataTemplate&gt;
            &lt;TextBlock Text="{Binding}"&gt;
                &lt;TextBlock.InputBindings&gt;
                    &lt;MouseBinding
                        Gesture="LeftDoubleClick"                                          
                       Command="{Binding RelativeSource={RelativeSource AncestorType=Window},
                                         Path=DataContext.EnterCommand3}"  /&gt;
                &lt;/TextBlock.InputBindings&gt;
            &lt;/TextBlock&gt;
        &lt;/DataTemplate&gt;
    &lt;/ListBox.ItemTemplate&gt;
    

    </ListBox>

    ViewModel

    public class GetMailPopModel : ViewModelBase
    {
    public ICommand EnterCommand3 { get; set; }
    public ModelPop PopGet { get; set; }
    public ICommand EnterCommand2 { get; set; }
    public MainViewModel mvm = new MainViewModel();
    public Action CloseAction { get; set; }
    public int p {get;set;}

    ListMessage view = new ListMessage();
    
    public GetMailPopModel()
    {
        Pop3Client client = new Pop3Client();
        EnterCommand3 = new RelayCommand(arg =&gt; Mouse());
        pop = new ObservableCollection&lt;ModelPop&gt;();
        PopGet = new ModelPop { };
    }
    
    private int selectedIndex;
    public int SelectedIndex
    {
        get
        {
            return selectedIndex;
        }
        set
        {
            selectedIndex = value;
            OnPropertyChanged("SelectedIndex");
        }
    }
    

    public ObservableCollection<ModelPop> GetMailPop()
    {
    Pop3Client client = new Pop3Client();
    client.Connect("pop.yandex.ru", 995, true);
    client.Authenticate("test.testmy", "qwerty5");

        var Derec = client.GetMessageCount();
    
    
        for (int i = Derec; i &gt; 0; i--)
        {
    
            pop.Add(new ModelPop() { FromP = client.GetMessage(i).Headers.From.Address.ToString() });
    
        }
    
        return pop;
    }
    
    void Mouse()
    {
    
        ListMessage lv = new ListMessage();
    
        MessageView mv = new MessageView();
        Selected sel = new Selected();
        mv.DataContext = sel;
        p = SelectedIndex;
    
        mv.Show();
    
    
    }
    

    ViewModelBase.cs

    public class ViewModelBase : INotifyPropertyChanged
    {
    public ObservableCollection<ModelPop> pop { get; set; }

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    

    }

    ModalPop

    public class ModelPop : ViewModelBase
    {
    private string _PopFrom;
    public string PopFrom
    {
    get { return _PopFrom; }
    set
    {

            _PopFrom = value;
            OnPropertyChanged("_PopFrom");
        }
    }
    
    private string _PopPass;
    public string PopPass
    {
        get { return _PopPass; }
        set
        {
            _PopPass = value;
            OnPropertyChanged("_PopPass");
        }
    }
    
    
    public override string ToString()
    {
        return string.Format("{0}", FromP);
    }
    

    }

    Selected.cs

     public class Selected:ViewModelBase
    {

    public ModelPop PopGet { get; set; }
    public ICommand EnterCommand2 { get; set; }
    public ObservableCollection&lt;Message&gt; allmessage { get; set; }
    //public ObservableCollection&lt;ModelPop&gt; pop { get; set;}
    GetMailPopModel d = new GetMailPopModel();
    public Message message { get; set; }
    
    Pop3Client client = new Pop3Client();
    
    
    public Selected()
    {
        Pop3Client client = new Pop3Client();
        allmessage = new ObservableCollection&lt;Message&gt;();
        PopGet = new ModelPop { };
        PopGet.builder = new StringBuilder();
    
        GetMailPop();
        //Get();
    
    }
    
    public string GetMailPop()
    
    {
        Pop3Client client = new Pop3Client();
        client.Connect("pop.yandex.ru", 995, true);
        client.Authenticate("test.testmy", "qwerty5");
    
        var Derec = client.GetMessageCount();
    
    
        for (int i = Derec; i &gt; 0; i--)
        {
    
            allmessage.Add(client.GetMessage(i));
    
    
        }
    
    
        Message message = client.GetMessage(d.p);
    


  • QA Engineer

    For starters, into functions Mouse() Pollutant this.SelectedIndexand not count it out. ListView

    Then you have a new copy of the class. SelectedWho knows nothing about pThat's why he did this. p I need to tell you.

    Then it appears that Selected Reading new messages, although in principle they could be transmitted to him in the same designer (they are at that point in the classroom) GetMailPopModel! Call attention to the fact that you're in. Selected You're doing something. One more. copy GetMailPopModel, it shouldn't be necessary in principle.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2