Click here to Skip to main content
15,886,002 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Where to go to learn wpf? Pin
Vimalsoft(Pty) Ltd12-Jan-15 20:56
professionalVimalsoft(Pty) Ltd12-Jan-15 20:56 
AnswerRe: Where to go to learn wpf? Pin
Subramanyam Shankar17-Mar-15 1:04
professionalSubramanyam Shankar17-Mar-15 1:04 
QuestionMultithreaded Windows with Data Binding Pin
Dominick Marciano5-Jan-15 8:30
professionalDominick Marciano5-Jan-15 8:30 
AnswerRe: Multithreaded Windows with Data Binding Pin
Afzaal Ahmad Zeeshan9-Jan-15 8:41
professionalAfzaal Ahmad Zeeshan9-Jan-15 8:41 
AnswerRe: Multithreaded Windows with Data Binding Pin
SledgeHammer0111-Jan-15 8:37
SledgeHammer0111-Jan-15 8:37 
QuestionWPF DataGrid with ComBox Bound To Enums - Show Descriptions Pin
Kevin Marois5-Jan-15 7:03
professionalKevin Marois5-Jan-15 7:03 
AnswerRe: WPF DataGrid with ComBox Bound To Enums - Show Descriptions Pin
Pete O'Hanlon5-Jan-15 7:19
mvePete O'Hanlon5-Jan-15 7:19 
QuestionGetting error when i drag&drop in WPF Pin
VisualLive1-Jan-15 17:35
VisualLive1-Jan-15 17:35 
Hello,
i am developing a Form where the user need to drag and drop between Grid,StackPanel etc. some data you can see the code below :
<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>
    <Grid x:Name="box1" Grid.Row="0" AllowDrop="True" Drop="box_Drop" >
        <Grid x:Name="grid1" Background="Aqua" Margin="15" MouseMove="grid_MouseMove">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="28*" />
                <ColumnDefinition Width="445*" />
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0" Grid.Row="0">
                <TextBlock Text="1"></TextBlock>
            </Grid>
            <Grid Grid.Column="0" Grid.Row="1">
                <TextBlock Text="2"></TextBlock>
            </Grid>
            <Grid Grid.Column="0" Grid.Row="2">
                <TextBlock Text="3"></TextBlock>
            </Grid>
            <StackPanel Name="firsts" AllowDrop="True" Drop="box_Drop_first" Grid.Row="0" Grid.Column="1"  >

                <StackPanel MouseMove="grid_MouseMove_first" Name="firstsrow"  Margin="2" Background="Beige" Height="17"></StackPanel>
                </StackPanel>
            <StackPanel Name="seconds" AllowDrop="True" Grid.Row="1" Grid.Column="1" Drop="box_Drop_first"  >
                <StackPanel MouseMove="grid_MouseMove_first" Name="secondrow"  Margin="2" Background="Gainsboro" Height="17"></StackPanel>
                </StackPanel>
            <StackPanel Name="thirds" AllowDrop="True" Grid.Row="2" Grid.Column="1" Drop="box_Drop_first" >
                <StackPanel MouseMove="grid_MouseMove_first"  Name="thirdrow"  Margin="2" Background="Red" Height="17"></StackPanel>
                </StackPanel>
        </Grid>
    </Grid>
    <Grid x:Name="box2" Grid.Row="1" AllowDrop="True" Drop="box_Drop">
        <Grid x:Name="grid2" Background="blue" Margin="15" MouseMove="grid_MouseMove">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="28*" />
                <ColumnDefinition Width="445*" />
            </Grid.ColumnDefinitions>
        </Grid>
    </Grid>
    <Grid x:Name="box3" Grid.Row="2" AllowDrop="True" Drop="box_Drop">
        <TextBlock  Text="3" HorizontalAlignment="Left" Width="30" Margin="10,0,0,77"></TextBlock>
        <Grid x:Name="grid3" Background="green" Margin="15" MouseMove="grid_MouseMove">
        </Grid>

    </Grid>

</Grid>


and the code Behind :
public partial class Window1 : Window
  {
      public Window1()
      {
          InitializeComponent();
      }


      private void grid_MouseMove(object sender, MouseEventArgs e)
      {
          // Get the grid that is the source of drag
          Grid selectedGrid = sender as Grid;

          if (selectedGrid != null && e.LeftButton == MouseButtonState.Pressed)
          {
              // Add that grid as drag source and data
              DragDrop.DoDragDrop(selectedGrid, selectedGrid, DragDropEffects.Move);
          }
      }

      private void box_Drop(object sender, DragEventArgs e)
      {
          // Get the selected box in which the object is being dropped
          Grid selectedBox = sender as Grid;

          if (selectedBox != null)
          {
              // Get that data that is being dropped - in this case, the grid from other box
              Grid droppedGrid = (Grid)e.Data.GetData(typeof(Grid));

              // We need to remove the dragged grid from it's source box in order to be able to add it to selected box
              Grid sourceBox = (Grid)droppedGrid.Parent;
              // Remove the dropped grid from source box
              sourceBox.Children.Remove(droppedGrid);

              // We need to remove the other grid from the selected box in order to be able to move it to source box
              // Get existing child element, the box has only one child - the grid that we need
              Grid existingChild = (Grid)selectedBox.Children[0];
              // Remove existing child grid from selected box
              selectedBox.Children.Remove(existingChild);

              // Finally, move grids to new boxes
              // Move existing child grid to source box
              sourceBox.Children.Add(existingChild);
              // Move the dropped grid to selected box
              selectedBox.Children.Add(droppedGrid);
          }
      }


      private void box_Drop_first(object sender, DragEventArgs e)
      {
          StackPanel selectedBox = sender as StackPanel;

          if (selectedBox != null)
          {
              // Get that data that is being dropped - in this case, the grid from other box
              StackPanel droppedGrid = (StackPanel)e.Data.GetData(typeof(StackPanel));

              // We need to remove the dragged grid from it's source box in order to be able to add it to selected box
            //  StackPanel sourceBox = new StackPanel();
            StackPanel  sourceBox = (StackPanel)droppedGrid.Parent;
              // Remove the dropped grid from source box
              sourceBox.Children.Remove(droppedGrid);

              // We need to remove the other grid from the selected box in order to be able to move it to source box
              // Get existing child element, the box has only one child - the grid that we need
              StackPanel existingChild = (StackPanel)selectedBox.Children[0];
              // Remove existing child grid from selected box
              selectedBox.Children.Remove(existingChild);

              // Finally, move grids to new boxes
              // Move existing child grid to source box
              sourceBox.Children.Add(existingChild);
              // Move the dropped grid to selected box
              selectedBox.Children.Add(droppedGrid);
          }
      }

      private void grid_MouseMove_first(object sender, MouseEventArgs e)
      {
          StackPanel selectedGrid = sender as StackPanel;

          if (selectedGrid != null && e.LeftButton == MouseButtonState.Pressed)
          {
              // Add that grid as drag source and data
              DragDrop.DoDragDrop(selectedGrid, selectedGrid, DragDropEffects.Move);
          }
      }
  }


The Grids "box1,box2,box3" are not movable instead their Childs are all movable (beacause in teh final project these will be populate with data then the user should be able to move and change the data when need) .
When i debug the project and i want move the StackPanel"firstrow,secondrow,thirdrow" between others StackPanel as"firsts,seconds.thirds" i got always an error as "Object reference not set to an instance of an object." or "Specified argument was out of the range of valid values.
Parameter name: index
" then don't allow to drag drop .
Sincerely i am new of WPF then sure i done some mistakes then i ask you kindly if you have some suggestion or better idea to work out this error.

Thank you in advance for the attention.

Sincerely
AnswerRe: Getting error when i drag&drop in WPF Pin
Afzaal Ahmad Zeeshan9-Jan-15 8:44
professionalAfzaal Ahmad Zeeshan9-Jan-15 8:44 
AnswerRe: Getting error when i drag&drop in WPF Pin
Vimalsoft(Pty) Ltd11-Jan-15 23:52
professionalVimalsoft(Pty) Ltd11-Jan-15 23:52 
QuestionLoop in Dispatch.Invoke Not Updating UI Pin
Dominick Marciano31-Dec-14 16:15
professionalDominick Marciano31-Dec-14 16:15 
QuestionSelectionChanged Event is not firing on View Load in MVVM Pin
Ashfaque Hussain29-Dec-14 20:15
Ashfaque Hussain29-Dec-14 20:15 
QuestionSilverlight? A future? Pin
Sean McPoland26-Dec-14 23:46
Sean McPoland26-Dec-14 23:46 
AnswerRe: Silverlight? A future? Pin
Richard MacCutchan26-Dec-14 23:57
mveRichard MacCutchan26-Dec-14 23:57 
AnswerRe: Silverlight? A future? Pin
Pete O'Hanlon27-Dec-14 2:03
mvePete O'Hanlon27-Dec-14 2:03 
QuestionWhat are those automation peer in WPF? Pin
Super Lloyd22-Dec-14 0:55
Super Lloyd22-Dec-14 0:55 
AnswerRe: What are those automation peer in WPF? Pin
Pete O'Hanlon22-Dec-14 1:32
mvePete O'Hanlon22-Dec-14 1:32 
AnswerRe: What are those automation peer in WPF? Pin
syed shanu23-Dec-14 17:42
mvasyed shanu23-Dec-14 17:42 
QuestionRouted Event No Responding Pin
Kevin Marois18-Dec-14 9:20
professionalKevin Marois18-Dec-14 9:20 
QuestionBest way to stroke a path with TWO colors? Pin
SledgeHammer0117-Dec-14 11:17
SledgeHammer0117-Dec-14 11:17 
SuggestionRe: Best way to stroke a path with TWO colors? Pin
Matt T Heffron17-Dec-14 11:39
professionalMatt T Heffron17-Dec-14 11:39 
GeneralRe: Best way to stroke a path with TWO colors? Pin
SledgeHammer0117-Dec-14 12:06
SledgeHammer0117-Dec-14 12:06 
QuestionWPF DataGrid Date Format Pin
Kevin Marois17-Dec-14 7:21
professionalKevin Marois17-Dec-14 7:21 
GeneralRe: WPF DataGrid Date Format Pin
PIEBALDconsult17-Dec-14 8:12
mvePIEBALDconsult17-Dec-14 8:12 
GeneralRe: WPF DataGrid Date Format Pin
Kevin Marois17-Dec-14 8:21
professionalKevin Marois17-Dec-14 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.