2012-05-24 8 views
5

मैं विंडोज मेट्रो स्टाइल एप्लिकेशन की कुछ प्राचीन नकल बनाने की कोशिश कर रहा हूं। मैंने अब तक क्या किया है ObservableCollection में विंडो में नई टाइल्स जोड़ रहा है, मैं अपना रंग बदल सकता हूं और ContextMenu का उपयोग करके उन्हें हटा सकता हूं। अब मैं खींचने और ड्रैगिंग (अर्ध पारदर्शी टाइल के साथ) के वास्तविक पूर्वावलोकन के साथ ड्रैग और ड्रॉप करना चाहता हूं। मैंने WPF में ड्रैगड्रॉप क्लास का वर्णन करने वाले कई ट्यूटोरियल्स का उपयोग करके इसे स्वयं करने की कोशिश की लेकिन मुझे यह स्वीकार करना है कि मैं इसे समझ नहीं पा रहा हूं और मुझे सहायता चाहिए। मैंने अनुसरण करने की कोशिश की: this tutorial। यहाँ मेरी एप्लिकेशन का एक screenshot है: screenshot और मेरे कोड:मेरे "मेट्रो स्टाइल" ऐप में डब्ल्यूपीएफ में ड्रैग और ड्रॉप कैसे करें?

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.Navigation; 
using System.Windows.Shapes; 
using System.Collections.ObjectModel; 

namespace Metro_Pawel_Michna 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     private ObservableCollection<myButton> _tiles = new ObservableCollection<myButton>(); 
     Random r = new Random(); 
     private int index = -1; 
     private List<Color> myColors = new List<Color>(); 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = _tiles; 
      webBrowser.Visibility = Visibility.Collapsed; 
      btnClose.Visibility = Visibility.Collapsed; 
      myColors.Add(Colors.DarkCyan); 
      myColors.Add(Colors.Black); 
      myColors.Add(Colors.DarkGoldenrod); 
      myColors.Add(Colors.DarkBlue); 
      myColors.Add(Colors.DarkGray); 
      myColors.Add(Colors.DarkKhaki); 
     } 

     private void btnAdd_Click(object sender, RoutedEventArgs e) 
     { 
      myButton b = new myButton(); 
      b.Content = txtUrl.Text; 
      b.MouseDoubleClick += new MouseButtonEventHandler(tileDbl_Click); 
      b.MouseRightButtonUp += new MouseButtonEventHandler(b_MouseRightButtonUp); 

      Color random = new Color(); 
      int losuj = r.Next(6); 
      b.colorIndex = losuj; 

      random = myColors.ElementAt(losuj); 

      LinearGradientBrush lgb = new LinearGradientBrush(Colors.White, random, 45); 
      lgb.StartPoint = new Point(-0.5,-0.5); 
      lgb.EndPoint = new Point(1, 1); 
      b.Background = lgb; 
      _tiles.Add(b); 
     } 

     private void tileDbl_Click(object sender, RoutedEventArgs e) 
     { 
      const string http = "http://"; 
      const string https = "https://"; 
      string address = (sender as Button).Content.ToString(); 

      if (String.Compare(http, 0, address, 0, 6) == 0 && address.Length > 7) webBrowser.Navigate(address); 
      else if (String.Compare(https, 0, address, 0, 7) == 0 && address.Length > 8) webBrowser.Navigate(address); 
      else webBrowser.Navigate("http://www.google.com/search?q=" + address); 

      tilesBox.Visibility = Visibility.Collapsed; 
      btnClose.Visibility = Visibility.Visible; 
      txtUrl.Visibility = Visibility.Collapsed; 
      btnAdd.Visibility = Visibility.Collapsed; 
      toolbar.HorizontalAlignment = HorizontalAlignment.Right; 
      webBrowser.Visibility = Visibility.Visible; 
     } 

     private void btnClose_Click(object sender, RoutedEventArgs e) 
     { 
      tilesBox.Visibility = Visibility.Visible; 
      btnClose.Visibility = Visibility.Collapsed; 
      txtUrl.Visibility = Visibility.Visible; 
      btnAdd.Visibility = Visibility.Visible; 
      toolbar.HorizontalAlignment = HorizontalAlignment.Left; 
      webBrowser.Visibility = Visibility.Collapsed; 
     } 

     private void Remove_Click(object sender, RoutedEventArgs e) 
     { 
      _tiles.RemoveAt(index); 
     } 

     private void b_MouseRightButtonUp(object sender, RoutedEventArgs e) 
     { 
      index = _tiles.IndexOf(sender as myButton); 
     } 

     private void ChangeColor_Click(object sender, RoutedEventArgs e) 
     { 
      myButton b = _tiles.ElementAt(index); 
      LinearGradientBrush lgb; 
      if (b.colorIndex != myColors.Count - 1) 
       lgb = new LinearGradientBrush(Colors.White, myColors.ElementAt(++b.colorIndex), 45); 
      else 
      { 
       lgb = new LinearGradientBrush(Colors.White, myColors.ElementAt(0), 45); 
       b.colorIndex = 0; 
      } 
      lgb.StartPoint = new Point(-0.5, -0.5); 
      lgb.EndPoint = new Point(1, 1); 
      b.Background = lgb; 
     } 
    } 
} 

XAML:

<Window x:Class="Metro_Pawel_Michna.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:Metro_Pawel_Michna="clr-namespace:Metro_Pawel_Michna" 
     Title="MainWindow" Height="350" Width="525" MinWidth="180" MinHeight="200"> 
    <DockPanel LastChildFill="True"> 
     <ToolBarTray Name="toolbar" DockPanel.Dock="Top"> 
      <ToolBar> 
       <TextBox Name="txtUrl">Type an URL</TextBox> 
       <Button Name="btnAdd" Click="btnAdd_Click">Add</Button> 
       <Button Name="btnClose" Click="btnClose_Click">Close</Button> 
      </ToolBar> 
     </ToolBarTray> 
     <WebBrowser Height="auto" Name="webBrowser" Width="auto" /> 
     <ScrollViewer> 
      <ItemsControl Name="tilesBox" ItemsSource="{Binding}"> 
       <ItemsControl.ContextMenu> 
        <ContextMenu Name="contextMenu"> 
         <MenuItem Header="Remove" Click="Remove_Click"/> 
         <MenuItem Header="Change color" Click="ChangeColor_Click"/> 
        </ContextMenu> 
       </ItemsControl.ContextMenu> 
       <ItemsControl.Resources> 
        <Style TargetType="{x:Type Metro_Pawel_Michna:myButton}"> 
         <Setter Property="Width" Value="120"/> 
         <Setter Property="Height" Value="120"/> 
         <Setter Property="Margin" Value="10"/> 
         <Setter Property="Foreground" Value="White" /> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate> 
            <Grid> 
             <Rectangle Fill="{TemplateBinding Background}" /> 
             <ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
            </Grid> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </ItemsControl.Resources> 

       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <WrapPanel /> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
     </ScrollViewer> 

    </DockPanel> 

</Window> 
+0

Impelment DragDrop.Drag और DragDrop.Drop। आप या तो आइटम या पुनरावर्तक नियंत्रण पर लागू कर सकते हैं। मुझे लगता है कि आपको एक सूची दृश्य में कदम उठाने की आवश्यकता होगी लेकिन स्क्रॉलव्यूवर भी इसका समर्थन कर सकता है। मैंने वस्तुओं में अधिक प्रत्यक्ष लेकिन अधिक प्रतिबंधक को कार्यान्वित किया। – Paparazzi

+0

एमएसडीएन मैग में चार्ल्स पेटज़ॉल्ड्स लेख देखें। आपको वहां अपना जवाब मिलना चाहिए। खेद है कि कोई भी नहीं, लेकिन 2010, 2011 के मुद्दों पर बैठे। जेजीडी –

उत्तर

4

आप, कुछ खींचें & ड्रॉप ढांचे का उपयोग करने के लिए घडि़याल-wpf-DragDrop की तरह इस कार्यक्षमता को लागू करने की कोशिश कर सकते हैं -

द गोंगसोल्शंस.पीएफएफ.ड्रागड्रॉप लाइब्रेरी WPF के लिए ड्रैग'ड्रॉप फ्रेमवर्क है। इसमें निम्नलिखित विशेषताएं हैं:

  • एमवीवीएम के साथ काम करता है: ड्रैग और ड्रॉप के लिए तर्क को व्यूमोडेल में रखा जा सकता है। कोडबेहिंड में कोई कोड नहीं रखा जाना चाहिए, इसके बजाय संलग्न गुण में व्यूमोडेल में ड्रैग हैंडलर/ड्रॉप हैंडलर से जुड़ने के लिए उपयोग किए जाते हैं।
  • एकाधिक चयनों के साथ काम करता है।
  • डेटा को फिर से ऑर्डर करने के लिए, या नियंत्रण के बीच डेटा को खींच सकता है।

http://code.google.com/p/gong-wpf-dragdrop/

पुनर्व्यवस्था आप के लिए ...

क्या देख रहे मामले में आप किसी भी ढांचे का उपयोग करने के लिए तो मैं तुम्हें सुझाव है कि इन लेखों के माध्यम से जाना नहीं चाहते है -

How can I drag and drop items between data bound ItemsControls? || WayBack Link

http://www.codeproject.com/Articles/37161/WPF-Drag-and-Drop-Smorgasbord

और & बूंद को लागू करने के लिए खींचें कुछ नियंत्रण के स्रोत कोड के माध्यम से जाने -

Drag and Drop Controls

+0

"मैं डेटा बाउंड आइटम्स नियंत्रण के बीच आइटम कैसे खींच और छोड़ सकता हूं?" यह लिंक टूटा हुआ है – FosterZ

+0

@FosterZ यह इंगित करने के लिए धन्यवाद, मैंने उस पृष्ठ के कैश किए गए संस्करण तक पहुंचने के लिए एक वेबैक लिंक जोड़ा है। – akjoshi

संबंधित मुद्दे