将主窗口实现更改为用户控件

时间:2014-01-18 07:17:54

标签: c# wpf xaml user-controls

我需要替换以下代码 usercontrol而不是主窗口(当然主窗口只是调用userControl),同时这样做我有以下问题。

目前我尝试在

的主窗口中添加以下代码

之后的

usercontrol的构造函数

public partial class UserControl: UserControl
    {
        private static MappingViewModelView _modelViewInstance;

        public UserControl()
        {
            InitializeComponent();
            _modelViewInstance = new MappingViewModelView();
            DataContext = _modelViewInstance;

            var source = Resources["source"] as CollectionViewSource;
            if (source != null)
                source.Source = _modelViewInstance.UserList;

            ListBox.SelectionChanged += listbox_SelectionChanged;

但是现在在用户控件中没有列表框的事件SelectionChanged(使用intlisense)只是ListBox.SelectionChangedEvent,这不适合主窗口的原始解决方案

ListBox.SelectionChanged + = listbox_SelectionChanged;

如果我输入确切的代码,则给出

错误:无法在静态上下文中访问非静态事件'SelectionChanged'

知道为什么列表框在用户控件中表现不同吗?

public partial class MainWindow : Window
{
   public ObservableCollection<User> _UsersList = new ObservableCollection<User>();
   private readonly Dictionary<string, string> _mapping = new Dictionary<string, string>();
   private const string DRAG_SOURCE = "DragSource";

   public MainWindow()
   {

      InitializeComponent();
      _UsersList.Add(new User { Name = "Jhon" });
      _UsersList.Add(new User { Name = "Mike" });
      _UsersList.Add(new User { Name = "Alex" });
      _UsersList.Add(new User { Name = "Darl" });

      CollectionViewSource source = this.Resources["source"] as CollectionViewSource;
      source.Source = _UsersList;
      ListBox.SelectionChanged += listbox_SelectionChanged;

      DataObject.AddCopyingHandler(text1, DragCopy);
      DataObject.AddCopyingHandler(text2, DragCopy);
   }

   public ObservableCollection<User> UserList
   {
      get { return _UsersList; }
   }


   private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
   {
     if (e.AddedItems.Count == 1)
     {
         if (ListBox.SelectedItems.Count > 0)
         {
            var mySelectedItem = ListBox.SelectedItem as User;
            if (mySelectedItem != null)
            {
                DragDrop.DoDragDrop(ListBox, mySelectedItem,
                DragDropEffects.Copy | DragDropEffects.Move);
            }
         }
     }
   }

   private void DropText_PreviewDragEnter(object sender, DragEventArgs e)
   {
      e.Effects = DragDropEffects.None;
   }

   private void DropText_PreviewDrop(object sender, DragEventArgs e)
   {
      var textbox = (TextBox)sender;

      if (!(textbox.Text.Length > 0))
      {
         DataObject data = e.Data as DataObject;
         User user = data.GetData(typeof(User)) as User;
         textbox.Tag = user;

         var name = user.Name;
         textbox.Text += name;
         textbox.Focus();
         textbox.CaretIndex = textbox.Text.Length;
         e.Handled = true;
         var remove = _UsersList.Remove((User)ListBox.SelectedItem);

         if (!_mapping.ContainsKey(textbox.Name))
             _mapping.Add(textbox.Name, name);
      }

      e.Handled = true;
   }

   private void DropText_PreviewDragOver(object sender, DragEventArgs e)
   {
      e.Handled = true;
   }

   private void ListBox_Drop(object sender, DragEventArgs e)
   {
       DataObject data = e.Data as DataObject;
       if (data != null)
       {
           User user = data.GetData(typeof(User)) as User;
           if (user != null && !_UsersList.Contains(user))
               _UsersList.Add(user);
       }


       TextBox txtBox = e.Data.GetData(DRAG_SOURCE) as TextBox;
       if (txtBox != null)
       {
           if (_mapping.ContainsKey(txtBox.Name))
               _mapping.Remove(txtBox.Name);
           txtBox.Dispatcher.BeginInvoke((Action)(() => { txtBox.Text = string.Empty; }), System.Windows.Threading.DispatcherPriority.Normal);
       }
       e.Handled = true; 
   }

   private void DragCopy(object sender, DataObjectCopyingEventArgs e)
   {
      if (e.IsDragDrop)
      {
         e.CancelCommand();
         TextBox txtBox = sender as TextBox;
         if (txtBox != null && txtBox.Tag != null)
         {
             DataObject dataObject = new DataObject(txtBox.Tag);
             dataObject.SetData(DRAG_SOURCE, txtBox);
             DragDrop.DoDragDrop(sender as DependencyObject, dataObject, DragDropEffects.Move | DragDropEffects.Copy);
         }
         e.Handled = true;
      }
   }

   private void DropText_TextChanged(object sender, TextChangedEventArgs e)
   {
      TextBox txtBox = sender as TextBox;
      if (txtBox.Text == string.Empty)
      {
         User user = txtBox.Tag as User;

         if(user != null && !_UsersList.Contains(user))
            _UsersList.Add(user);

         if (_mapping.ContainsKey(txtBox.Name))
             _mapping.Remove(txtBox.Name);
      }
   }
}


ListBox x:Name="ListBox" HorizontalAlignment="Left" Height="115"
                      VerticalAlignment="Top" Width="150" ItemsSource="{Binding Source={StaticResource source}}" 
                     DisplayMemberPath="Name"
                     AllowDrop="True" Drop="ListBox_Drop" />

<Window.Resources>
        <CollectionViewSource x:Key="source">
        </CollectionViewSource>
    </Window.Resources>



        <TextBox x:Name="text1"  
                  AcceptsReturn="True"
                  AllowDrop="True" 
                  PreviewDragEnter="DropText_PreviewDragEnter"
                  PreviewDrop="DropText_PreviewDrop"
                  PreviewDragOver="DropText_PreviewDragOver"
                  TextChanged="DropText_TextChanged"
                  Grid.Column="1"
                  HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" 
                  VerticalAlignment="Top" Width="120"/>

        <TextBox x:Name="text2"  
                  AcceptsReturn="True"
                  AllowDrop="True" 
                  PreviewDragEnter="DropText_PreviewDragEnter"
                  PreviewDrop="DropText_PreviewDrop"
                  PreviewDragOver="DropText_PreviewDragOver"
                  TextChanged="DropText_TextChanged"
                  Grid.Column="1"
                  HorizontalAlignment="Left" Height="23" TextWrapping="Wrap"
                  VerticalAlignment="Top" Width="120"/>

1 个答案:

答案 0 :(得分:1)

如果您将ListBox移动到UserControl,最可能的原因是 - 可能是偶然的 - 在此过程中更改了名称。
错误消息指向此方向,因为它将ListBox识别为类型,而不是UserControl上ListBox的具体实例的名称。