using ListBoxLinqBindingSample.Model; using ListBoxLinqBindingSample.SubWindow; using ListBoxLinqBindingSample.ViewModel; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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.Navigation; using System.Windows.Shapes; namespace ListBoxLinqBindingSample { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private static Duties _duties = new Duties(); public delegate void RefreshList(DutyType dutyType); public event RefreshList RefreshListEvent; public MainWindow() { InitializeComponent(); } internal static Duties Duties { get { return _duties; } set { _duties = value; } } private void RefreshListBox(DutyType dutyType) { lstType.SelectedItem = null; lstType.SelectedIndex = dutyType == DutyType.Inside ? 0 : 1; } private void btnAdd_Click(object sender, RoutedEventArgs e) { SubWindow.SubWindow subWindow = new SubWindow.SubWindow(); this.RefreshListEvent += new RefreshList(RefreshListBox); subWindow.UpdateActor = this.RefreshListEvent; subWindow.Show(); } private void lstType_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox lb = sender as ListBox; if (lb == null) return; if (lb.SelectedItem == null) return; ListBoxItem item = lb.SelectedItem as ListBoxItem; string dutyType = item.Content.ToString(); this.DataContext = from duty in _duties where duty.DutyType.ToString() == dutyType select duty; } private void lstMain_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox lb = sender as ListBox; if (lb == null) return; if (lb.SelectedItem == null) return; Duty duty = lb.SelectedItem as Duty; if (duty == null) return; MessageBox.Show($"{duty.Name} :: {duty.DutyType}", "Selected duty"); } } }